AsyncIO and the Event Loop Explained

  Рет қаралды 25,804

ArjanCodes

ArjanCodes

Күн бұрын

Over the years, I’ve produced several videos about AsyncIO. Today, however, I’m adopting a new approach where I explain the event loop in depth. I’ll delve deeper into asynchronous programming, focusing specifically on how the event loop operates behind the scenes.
🔥 GitHub Repository: git.arjan.codes/2024/asyncio_...
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
💻 ArjanCodes Blog: www.arjancodes.com/blog
✍🏻 Take a quiz on this topic: www.learntail.com/quiz/ccixtk
Try Learntail for FREE ➡️ www.learntail.com/
🎓 Courses:
The Software Designer Mindset: www.arjancodes.com/mindset
The Software Architect Mindset: www.arjancodes.com/architect
Next Level Python: Become a Python Expert: www.arjancodes.com/next-level...
The 30-Day Design Challenge: www.arjancodes.com/30ddc
🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
Social channels:
💬 Discord: discord.arjan.codes
🐦X: / arjancodes
🌍LinkedIn: / arjancodes
🕵Facebook: / arjancodes
📱Instagram: / arjancodes
♪ Tiktok: / arjancodes
👀 Code reviewers:
- Yoriz
- Ryan Laursen
- Dale Hagglund
- Kit Hygh
- Alexander Milden
- Bean
🎥 Video edited by Mark Bacskai: / bacskaimark
🔖 Chapters:
0:00 Intro
1:02 The Basics of Asyncio
3:43 Synchronous Server
4:53 Asynchronous Server
8:05 Why Asyncio?
9:05 AIO Libraries
12:38 Outro
#arjancodes #softwaredesign #python
DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

Пікірлер: 57
@ArjanCodes
@ArjanCodes 2 ай бұрын
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
@Testmail-lu8tl
@Testmail-lu8tl 2 ай бұрын
Please make more videos on asyncio and asynchronous programming in python, also how newer versions of python dealing with GIL
@user-ng2iv7qy8c
@user-ng2iv7qy8c 2 ай бұрын
8:09 threads doesnt have limitations because of GIL if we speak about concurrency and compare it with asyncio. GIL just allows only one thread per process to run at a time. But process can still switch between threads, just like asyncio event loop switch between tasks but within single thread. GIL just doesnt allow parallel execution of threads on multiple system threads/cores/CPUs as u like. Its nothing to do with IO, but with CPU time
2 ай бұрын
Yes but as you cannot run threads in parallel and use them for only IO a lighter solution in terms of context switching is better and scales better
@sinancetinkaya
@sinancetinkaya 2 ай бұрын
With threads we have more serious problems like thread safety, object sharing, race conditions. AsyncIO is a life saver.
@cheatoffchannel6320
@cheatoffchannel6320 2 ай бұрын
​@@sinancetinkaya lol, race conditions are also possible with asyncio
@motbus3
@motbus3 2 ай бұрын
@@cheatoffchannel6320 you can have race conditions with a single thread and no concurrency at all for x in loop: loop.append(1)
@Han-ve8uh
@Han-ve8uh 2 ай бұрын
Thanks for clarifying, exactly what i thought too. It's confusing to say GIL is a disadvantage of threading with IO bound tasks when asyncio is also doing 1 thing at any moment to the same effect of GIL. I wish cooperative vs preemptive multitasking was mentioned so audience can learn for themselves the differences in whether it's the process(asyncio) or system(multithread) that initiates the context switch
@abdelghafourfid8216
@abdelghafourfid8216 2 ай бұрын
I'd love going more in depth about advanced features and functionalities like Semaphore etc
@abomayeeniatorudabo8203
@abomayeeniatorudabo8203 2 ай бұрын
Great video. Exactly what I needed for my current task.
@ArjanCodes
@ArjanCodes 2 ай бұрын
Glad it was helpful!
@__mostafa__
@__mostafa__ 2 ай бұрын
What's better than Arjan uploading a new video?🤩
@ArjanCodes
@ArjanCodes 2 ай бұрын
Ahah! Thank you for the support :)
@davidl3383
@davidl3383 2 ай бұрын
Exactly my week's problem. Not a simple library, but very interesting and powefull.Thank you Arjan
@ArjanCodes
@ArjanCodes 2 ай бұрын
Glad it was helpful!
@cristeapaul5634
@cristeapaul5634 2 ай бұрын
Thank you for this video! Really helpful! I would also appreciate if you can deep dive in asyncio library and discuss about canceled tasks and proper exception handling.😁
@paxdriver
@paxdriver 2 ай бұрын
Thank you so so much for this channel. I'm building an app serving fastapi json data from Python parsers I built from tips from this channel. You introduce newbies to so many advanced tools that seem daunting, it's such a huge help
@ArjanCodes
@ArjanCodes 2 ай бұрын
Glad to hear the videos have had a positive impact on your journey. Cheers!
@crysiank
@crysiank 2 ай бұрын
I fully agree that Python's standard library needs more async support.
@James-vd3xj
@James-vd3xj 2 ай бұрын
I'm curious why it was never implemented as a standard from the get-go. Do you know if there were technological limitations or other reasons at the time that prevented async from becoming part of Python's standard library earlier? It would be great to understand the historical context to see how we might push for more comprehensive async support in the future.
@cheatoffchannel6320
@cheatoffchannel6320 2 ай бұрын
@@James-vd3xj because threading is simpler solution and doesn't require special syntax. Main benefit of async is that it is more lightweight, that's it.
@James-vd3xj
@James-vd3xj 2 ай бұрын
@@cheatoffchannel6320 Thanks for the brief explanation.
@alonnajman6555
@alonnajman6555 Ай бұрын
I agree as well
@youtubeenjoyer1743
@youtubeenjoyer1743 6 күн бұрын
Because javascript people bullied python people into adding async/await syntax. Even Guido himself couldn't defend it, so he left. Sad!
@augustusbatilojibba1405
@augustusbatilojibba1405 2 ай бұрын
@ArjanCodes Will you make an async video in python using Django Rest Framework? I will be happy if you make one.
@user-tt4hp9yb2x
@user-tt4hp9yb2x 2 ай бұрын
Great video Arjan, but one question/request: What is that kind of diagram you show around 2:50 called? I seems great for communicating high-level logic, and you should consider making a video about those. Greetings from Copenhagen, and have a great day!
@Han-ve8uh
@Han-ve8uh 2 ай бұрын
en.wikipedia.org/wiki/Sequence_diagram
@kellymoses8566
@kellymoses8566 2 ай бұрын
Sequence diagram
@paxdriver
@paxdriver 2 ай бұрын
Javascript we workers with fast api is an awesome combo. Promises kick ass.
@loupniko
@loupniko 2 ай бұрын
Would be great if you'd make a video on new Python 3.13 features. Especially on --disable-gil option and it's impact on performance.
@jam1239665
@jam1239665 2 ай бұрын
can you show how to do async calls with AWS resources?
@walid7189
@walid7189 2 ай бұрын
if I am streaming a decent amount of data from lets say multiple IoT devices to a rpi. I want to serve the data in real time to the server. If I use the concurrent async way, each job will get executed (or literally sent to the server) while blocking each other and at a point won't be displayed in real time? Am I getting this right?
@slm6873
@slm6873 2 ай бұрын
I was surprised the other day, that an mmap to mmap copy (float32->int16 of very large files) was much slower than having async read and write tasks, no matter how I chunked things.
@_Gabriwl_
@_Gabriwl_ 7 күн бұрын
Where do i find the course that teach me how to make this own implementation of web server using asyncio??
@MicheleHjorleifsson
@MicheleHjorleifsson 2 ай бұрын
With the drive towards API consumption all around us i think having concurrency in the stdlib would be a smart move for the Python folks
@greob
@greob 2 ай бұрын
I often write synchronous code, and after a while I realize that I need it to be asynchronous. It’s often painful to redo the entire thing with async in mind, and sometimes I just resort to using threads instead to encapsulate my synchronous code. I always wonder if that’s the right thing to do.
@4355784
@4355784 2 ай бұрын
With async libraries on mysql database. I had to go very low (commit and rollback) instead of simple annotation like in spring boot. It could be cool to have the same annotation with python libs.
@eyaldahari
@eyaldahari 2 ай бұрын
Thanks for that video, Arjan. I still do not understand one thing. If I have an IO bound method or library I am using which do not support asyncio is there a way to wrap it to make it async?
@cheatoffchannel6320
@cheatoffchannel6320 2 ай бұрын
@@eyaldahari asgiref standard library module has decorator sync_to_async, but without proper internal implementation it won't give any benefit in concurrency, it just allows to run from async context. For external libs the best way is to use threading, many libs are actually thread-safe, or you can lock critical sections by yourself.
@zakk6182
@zakk6182 2 ай бұрын
Cool
@v.s.naaviinesh
@v.s.naaviinesh 2 ай бұрын
cool
@marckiezeender
@marckiezeender 2 ай бұрын
I wish there were good async desktop app frameworks.
@gper9
@gper9 2 ай бұрын
Tornado is another great async web framework
@_DRMR_
@_DRMR_ 2 ай бұрын
Tornado did async before it was cool!
@hansdietrich1496
@hansdietrich1496 2 ай бұрын
gevent for the win. Same technique under the hood, but all async/await stuff just magically happens.
@raphaelobadia7664
@raphaelobadia7664 2 ай бұрын
Actually Fastapi use starlette that use anyo.
@aflous
@aflous 2 ай бұрын
anyio*
@JohnWalz97
@JohnWalz97 2 ай бұрын
almost first 😛
@bingzhao9314
@bingzhao9314 2 күн бұрын
can u support Chinese cc.i think Chinese software developer quantities are in top 3. thanks
5 Tips for Building Powerful Data Dashboards in Python
19:05
ArjanCodes
Рет қаралды 28 М.
Protocols vs ABCs in Python - When to Use Which One?
15:31
ArjanCodes
Рет қаралды 34 М.
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН
Самый Молодой Актёр Без Оскара 😂
00:13
Глеб Рандалайнен
Рет қаралды 12 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 63 МЛН
import asyncio: Learn Python's AsyncIO #2 - The Event Loop
36:07
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
12:35
Next-Level Concurrent Programming In Python With Asyncio
19:19
ArjanCodes
Рет қаралды 169 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 228 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 199 М.
Has Generative AI Already Peaked? - Computerphile
12:48
Computerphile
Рет қаралды 901 М.
Python Asyncio, Requests, Aiohttp | Make faster API Calls
17:56
Patrick Collins
Рет қаралды 127 М.
Python 3.12 Generic Types Explained
18:27
ArjanCodes
Рет қаралды 59 М.
What does larger scale software development look like?
24:15
Web Dev Cody
Рет қаралды 1,3 МЛН
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН