AsyncIO and the Event Loop Explained

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!

Пікірлер: 54

  • @ArjanCodes
    @ArjanCodesАй бұрын

    💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.

  • @Testmail-lu8tl
    @Testmail-lu8tl2 ай бұрын

    Please make more videos on asyncio and asynchronous programming in python, also how newer versions of python dealing with GIL

  • @__mostafa__
    @__mostafa__2 ай бұрын

    What's better than Arjan uploading a new video?🤩

  • @ArjanCodes

    @ArjanCodes

    2 ай бұрын

    Ahah! Thank you for the support :)

  • @abdelghafourfid8216
    @abdelghafourfid8216Ай бұрын

    I'd love going more in depth about advanced features and functionalities like Semaphore etc

  • @abomayeeniatorudabo8203
    @abomayeeniatorudabo82032 ай бұрын

    Great video. Exactly what I needed for my current task.

  • @ArjanCodes

    @ArjanCodes

    2 ай бұрын

    Glad it was helpful!

  • @user-ng2iv7qy8c
    @user-ng2iv7qy8c2 ай бұрын

    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

    Ай бұрын

    @@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

    Ай бұрын

    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

  • @davidl3383
    @davidl33832 ай бұрын

    Exactly my week's problem. Not a simple library, but very interesting and powefull.Thank you Arjan

  • @ArjanCodes

    @ArjanCodes

    2 ай бұрын

    Glad it was helpful!

  • @paxdriver
    @paxdriver2 ай бұрын

    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!

  • @cristeapaul5634
    @cristeapaul56342 ай бұрын

    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.😁

  • @crysiank
    @crysiank2 ай бұрын

    I fully agree that Python's standard library needs more async support.

  • @James-vd3xj

    @James-vd3xj

    Ай бұрын

    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

    Ай бұрын

    @@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

    Ай бұрын

    @@cheatoffchannel6320 Thanks for the brief explanation.

  • @alonnajman6555

    @alonnajman6555

    21 күн бұрын

    I agree as well

  • @augustusbatilojibba1405
    @augustusbatilojibba14052 ай бұрын

    @ArjanCodes Will you make an async video in python using Django Rest Framework? I will be happy if you make one.

  • @jam1239665
    @jam12396652 ай бұрын

    can you show how to do async calls with AWS resources?

  • @walid7189
    @walid71892 ай бұрын

    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?

  • @user-tt4hp9yb2x
    @user-tt4hp9yb2x2 ай бұрын

    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

    Ай бұрын

    en.wikipedia.org/wiki/Sequence_diagram

  • @kellymoses8566

    @kellymoses8566

    Ай бұрын

    Sequence diagram

  • @paxdriver
    @paxdriver2 ай бұрын

    Javascript we workers with fast api is an awesome combo. Promises kick ass.

  • @slm6873
    @slm68732 ай бұрын

    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.

  • @loupniko
    @loupnikoАй бұрын

    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.

  • @greob
    @greobАй бұрын

    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.

  • @zakk6182
    @zakk61822 ай бұрын

    Cool

  • @v.s.naaviinesh
    @v.s.naaviinesh2 ай бұрын

    cool

  • @4355784
    @43557842 ай бұрын

    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

    Ай бұрын

    @@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.

  • @MicheleHjorleifsson
    @MicheleHjorleifssonАй бұрын

    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

  • @marckiezeender
    @marckiezeender2 ай бұрын

    I wish there were good async desktop app frameworks.

  • @hansdietrich1496
    @hansdietrich14962 ай бұрын

    gevent for the win. Same technique under the hood, but all async/await stuff just magically happens.

  • @gper9
    @gper92 ай бұрын

    Tornado is another great async web framework

  • @_DRMR_

    @_DRMR_

    2 ай бұрын

    Tornado did async before it was cool!

  • @raphaelobadia7664
    @raphaelobadia76642 ай бұрын

    Actually Fastapi use starlette that use anyo.

  • @aflous

    @aflous

    2 ай бұрын

    anyio*

  • @JohnWalz97
    @JohnWalz972 ай бұрын

    almost first 😛