Python Asynchronous Programming - AsyncIO & Async/Await

Поделиться
HTML-код
  • Опубликовано: 15 янв 2025
  • In today's video, I'll be talking to you about asynchronous programming in python. This Python Async tutorial will cover the 'async' and 'await' keyword, coroutines, futures and tasks, and some basic features from the asyncio module in Python. This video is for intermediate programmers, and it's recommended you have Python 3.7 or above.
    💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" algoexpert.io/...
    📄 Documentation
    AsyncIO: docs.python.or...
    ⭐️ Timestamps ⭐️
    01:43 - Synchronous vs Asynchronous
    04:30 - Coroutines
    07:35 - Async Event-Loop
    08:58 - Async/Await Keywords
    12:12 - Tasks
    17:00 - Async Example
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    💰 Courses & Merch 💰
    💻 The Fundamentals of Programming w/ Python: tech-with-tim....
    👕 Merchandise: teespring.com/...
    🔗 Social Medias 🔗
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/tec...
    🔊 Podcast: anchor.fm/tech...
    🎬 My RUclips Gear 🎬
    🎥 Main Camera (EOS Canon 90D): amzn.to/3cY23y9
    🎥 Secondary Camera (Panasonic Lumix G7): amzn.to/3fl2iEV
    📹 Main Lens (EFS 24mm f/2.8): amzn.to/2Yuol5r
    🕹 Tripod: amzn.to/3hpSprv
    🎤 Main Microphone (Rode NT1): amzn.to/2HrZxXc
    🎤 Secondary Microphone (Synco Wireless Lapel System): amzn.to/3e07Swl
    🎤 Third Microphone (Rode NTG4+): amzn.to/3oi0v8Z
    ☀️ Lights: amzn.to/2ApeiXr
    ⌨ Keyboard (Daskeyboard 4Q): amzn.to/2YpN5vm
    🖱 Mouse (Logitech MX Master): amzn.to/2HsmRDN
    📸 Webcam (Logitech 1080p Pro): amzn.to/2B2IXcQ
    📢 Speaker (Beats Pill): amzn.to/2XYc5ef
    🎧 Headphones (Bose Quiet Comfort 35): amzn.to/2MWbl3e
    🌞 Lamp (BenQ E-reading Lamp): amzn.to/3e0UCr8
    🌞 Secondary Lamp (BenQ Screenbar Plus): amzn.to/30Dtafi
    💻 Monitor (BenQ EX2780Q): amzn.to/2HsmUPZ
    💻 Monitor (LG Ultrawide 34WN750): amzn.to/3dSD7tS
    🎙 Mic Boom Arm (Rode PSA 1): amzn.to/30EZw9m
    🎚 Audio Interface (Focusrite Scarlet 4i4): amzn.to/2TjXsih
    💸 Donations 💸
    💵 One-Time Donations: www.paypal.com...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    Asynchronous programming
    Coroutines
    Futures in Python
    Tasks in Python
    Python tutorial
    Tech With Tim
    ⭐️ Hashtags ⭐️
    #Programming #Asynchronous #TechWithTim

Комментарии • 383

  • @PeterJacobFloratos
    @PeterJacobFloratos Год назад +18

    A quick story. I started programming 4 years ago at Tim was the first instructor in this vast sea of programming. When I learned the basic I kinda move on and start my own journey. Today in my work I need to use asynchronous programming and of course I knew the place to learn. Tim you are amazing, thank you so much for the knowledge you have given us!

  • @yoyoteo99
    @yoyoteo99 3 года назад +363

    "a programmer had a problem and he thought to solve it with thread.
    has problems. two Now he"

  • @paccooper3019
    @paccooper3019 2 года назад +4

    Seriously, I don't know what I would do without your tutorials

  • @enamulmajid8424
    @enamulmajid8424 3 года назад +13

    Just started with asynchronous programming in python, and this video of yours helps me to get comfortable with the asynchronous concept.
    Thank you @tim
    (well, I must include, I have a good grasp in the primary concepts of python, then I headed to the asynchronous concept in python)

  • @TheKiryu.
    @TheKiryu. 3 года назад +461

    It would be great to learn about the differences between acyncio, multiprocessing and threading

    • @JoshelinRico
      @JoshelinRico 3 года назад +64

      asyncio and threading are almost identical, but threading is more intuitive. They are mainly used for I/O bound tasks.
      Mulltiprocessing is very different. It bypasses the GIL (look it up if you don't know what it is). It is mainly used to improve performance with parallel programming.

    • @sleepymarauder4178
      @sleepymarauder4178 3 года назад +54

      Both asyncio and threading are concurrent.
      Threading is being handled by the OS and AsyncIO is being handled within python ( the async event loop ).
      Threads can execute small operations to speeds things up ( e.g. adding more robots to handle the work, difficult to oversee and manage when program grows big. Also race conditions where: comes out weird the outcome )
      asyncio is done by using an event loop where task are being awaited ( e.g. non-blocking! tasks that are fired up and deliver some result while the program is doing other things )
      At the end of the event loop ( yes it is a loop, all the results are gathered from the loop and it starts over again )
      If tasks are blocking, asyncio isn't the right tool at all. Luckily you can also "throw" blocking tasks into a different executor so that they dont slow down your async stuff.
      Multiprocessing is using multiple processes to do the grunt work. It kind of bypasses the GIL by having more processes with another GIL.
      Drawback can be copied memory and such.
      Basically every technique has an advantage and a drawback. I do like asyncio because it is a simple solution to a complex problem.
      The thing is that when using an Executor you can test different scenario's within Python because you can swap out multiprocessing, threading and asyncio easily.
      Test and see what does what.

    • @sadhlife
      @sadhlife 3 года назад +14

      @@sleepymarauder4178 thank you.
      i see so many people spreading knowledge about threading vs. async which is wrong. good to see at least there's people out there explaining things completely and correctly.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 3 года назад +2

      Threading and multiprocessing are preemptive, coroutines are not. Coroutines and threading are default-share-everything, multiprocessing is default-share-nothing. Coroutines are easier to debug than threads.

    • @imnormal-sl8mj
      @imnormal-sl8mj 3 года назад +1

      ​@@sleepymarauder4178 do you know when is it better to use threading over async?

  • @ZenshiOff
    @ZenshiOff 3 года назад +341

    I literally learnt this through making Discord Bots

    • @wannabedev2452
      @wannabedev2452 3 года назад +16

      Same...

    • @pic01
      @pic01 3 года назад +18

      Me too (although I didn't get a good in-depth explanation as this). So thanks so much Tim

    • @bamboo6392
      @bamboo6392 3 года назад

      Same here

    • @Broso56
      @Broso56 3 года назад +9

      lol yeah like 3 months ago i learned python and by day 2 i started making my discord bot and i saw this and started wondering what exactly asynchronous programming is, even though i knew thats what i've been doing, i never knew what exactly it was since i never really thought much of it & it was my first project lol

    • @pic01
      @pic01 3 года назад

      @@Broso56 that's actually the same as me

  • @kitetm7596
    @kitetm7596 3 года назад +30

    I wanted this for so long thank you so much! :)

  • @dovids.greenberger435
    @dovids.greenberger435 3 года назад +179

    Idea for future series: Go through the source code for popular modules and show us how they were created. Would be a great learning experience.

    • @sadhlife
      @sadhlife 3 года назад +4

      Absolutely! +1

    • @samgallon1273
      @samgallon1273 3 года назад +1

      Yes! Tim can start with basic string functions like .join() or .split() and show us it's code.

    • @LovePeace2025
      @LovePeace2025 2 года назад +1

      you could do it yourself. Just go through the source code.

    • @BiologyIsHot
      @BiologyIsHot 10 месяцев назад

      ​​@@samgallon1273 very primitive builtins are usually a terrible example for this if you're trying to learn python since they're almost always implemented in C, not Python. This is part of the reason why it's hard to re-implement basic python functions faster than the default implementations--theyre usually implemented in C at the very basic level of the CPython interpreter. On top of that they're usually also very well implemented to be optimized for their particular use case(s), but the mere fact that they don't really do much in Python is a huge advantage.

  • @nogamenolife9182
    @nogamenolife9182 3 года назад +1

    Oh, man. I needed this. I watched lots of videos but none of them were as good and simple-explained as yours.

  • @neonsilver1936
    @neonsilver1936 2 года назад +1

    I came to this video to try and find out what is mean by "asynchronous iterator", and while this isn't exactly that, I think I understand a little better what the "asynchronous" part means. This is an extremely powerful resource, I'm very glad I watched this. I just leveled up in my Python substantially, lol

  • @99ansh
    @99ansh 3 года назад +1

    One of the best videos to get started with asyncio!

  • @future9715
    @future9715 3 года назад +58

    asyncio is pretty useful when you're coding server's responses etc. While one request is being processed, the other one begins.

    • @SA-oj3bo
      @SA-oj3bo 3 года назад +1

      Hi do you know maybe some good tutorials how to use asyncio to handle multiple tcp sockets on a server? What would be the advantage to handle multiple sockets with asyncio compared to handle multiple server sockets with threads?

    • @quanuongminh1458
      @quanuongminh1458 Год назад

      Can you compare the asyncio vs thread pool executor for me. I'm creating a script about sending and receving the request but when using TPE, the thread is locked for no reason

  • @rok3rGaming
    @rok3rGaming 9 дней назад

    I'm a two year experienced Software developer trying hard to get into employment and this video just gave me some confident boost . Thanks Tim

  • @krause3d
    @krause3d 3 года назад +5

    Great stuff! I've been reading up on this for a few days now and your breakdown has been the clearest explanation. Thank you.

  • @Glaszg
    @Glaszg 3 года назад

    Was looking for such video for months now. No recent clearly explained videos on this topic out there until now. Thanks!

  • @qwe-xq8ne
    @qwe-xq8ne 3 года назад

    I had trouble understanding from other videos so I searched "Async Await Tech With Tim". Now it is much clearer. Thank you.

  • @MuhammadShahidkhan
    @MuhammadShahidkhan 3 года назад +17

    Hey Tim, nice to meet you! I just found your channel, love what you're doing!
    I like how clear and detailed your explanations are as well as the depth of knowledge you have surrounding the topic! Since I run a tech education channel as well, I love to see fellow Content Creators sharing, educating, and inspiring a large global audience. I wish you the best of luck on your RUclips Journey, can't wait to see you succeed! Your content really stands out and you've put so much thought into your videos, I applaud you on that!
    Cheers, take care, and keep up the great work ;)

    • @TechWithTim
      @TechWithTim  3 года назад +3

      Really appreciate the comment :)

  • @phreakyphoenix
    @phreakyphoenix Год назад

    This is an absolutely spendid demonstration of asyncio, I needed to use it today and had no clue how to go about it, now I'm off to testing my understanding on my product. Thanks Tim.

  • @alisahibqadimov7659
    @alisahibqadimov7659 Год назад

    Best explanation on RUclips. Thanks TIM !!

  • @superthaix
    @superthaix 10 месяцев назад

    Absolutely fantastic and best video explanation of the asyncio and the keywords. Actually , very clear and to the point and thanks for not using ridiculous analogies.

  • @carewen3969
    @carewen3969 3 года назад

    I'm about a month into learning Python. That was a great explanation and gives me a foundation to dig deeper. It shows me how I can have multiple things happening at the same time, and the concept of futures really is exciting; in that I can see how the idea in my mind can actually be built.

  • @aryan2628
    @aryan2628 3 года назад +3

    Awesome!! Was waiting for this from soo long

  • @sachinsinghal13
    @sachinsinghal13 3 года назад +9

    Thanks Tim, this was a really helpful and useful and awesome tutorial, I really enjoyed it. I 'await' fot the next tutorial!!!

  • @vikrantbhadouriya
    @vikrantbhadouriya Год назад

    Hey Tim, tbh I tried to watch a few videos on asyncio, tried reading a few articles, but your video was straight and simple! I feel better that I finally understood (even a little) about how asynchronous programming works in Python. Cheers!

  • @saikoome
    @saikoome Год назад

    Thanks for this Tim!

  • @kassemkanaan4496
    @kassemkanaan4496 3 года назад +3

    i were searching for it this morning , thankyou!

  • @JackEnneking
    @JackEnneking 2 года назад

    This is the first time I really understood the basic mechanics of asyncio. Thank you!

  • @Kaeribana
    @Kaeribana 2 года назад

    I learned asynchronous programming on hard mode, means that when I started my first asyncio project (discord bot) I knew nothing about it, and barely a beginner in programming world and python In general i struggled days even for simple stuff, though this made me really learn not just scrolling through videos and remember nothing after over one year of working at my project ( which is alive even now, because I'm always performing maintenance checks on my bot) I still feel like I have a lot to learn even if I'm able to write a generous amount of code without "cheating" (searching for answers or something)
    Great video for people who want to dive more deeper about asynchronous code

  • @kacper2246
    @kacper2246 2 года назад

    very thankful for this video
    solved the issue i was struggling with for a few hours

  • @hamidrezaraei8404
    @hamidrezaraei8404 2 года назад +1

    Thanks man, I have toi say one of the best tutorial over the whole asyncio python package ;)

  • @kizernis
    @kizernis Год назад

    Finally, someone explained this so I could understand. Liked, subscribed 👍

  • @beneditomarques4062
    @beneditomarques4062 2 года назад

    Best video about asyncio in python. Congratulations!

  • @hasanfares5774
    @hasanfares5774 3 года назад +3

    thanks my brain was awaiting this video now I can move to other tasks😉😎

  • @imherovirat
    @imherovirat 3 года назад +6

    Man what the actual F, I was learning to make discord bot and now I get this video, you must be reading my head, Thanks man learning a new topic fromfav youtuber is really Awesome!

  • @werner6816
    @werner6816 2 года назад +1

    the younger version of Joaqin Phoenix explains asynch programming better than our university professors :-)

  • @RafaelGuedes84
    @RafaelGuedes84 2 года назад +2

    Hi Tim, thanks a bunch for this video. I was struggling to understand asyncio and you made it simple to grasp. God bless you!

  • @RoshanDhakal-vi1nh
    @RoshanDhakal-vi1nh Год назад +1

    thanks dude, i know you might not see this but thanks for your help.

  • @rutvikrana512
    @rutvikrana512 3 года назад +4

    I am used to code in Dart while making apps in Flutter. async and await are used very often in it, I have good understanding of asynchronous programming because of it.

    • @sadhlife
      @sadhlife 3 года назад +3

      do understand that python i/o functions are not async by default, so things like open() and time.sleep() and many many many others don't play well with async/await, as they block the entire event loop.
      the idea is the same between dart and python, but you'll have to use asyncio-specific async functions instead of the builtin python ones. keep that in mind :)

  • @omidasadi2264
    @omidasadi2264 Год назад

    thanks bro.... shortest tutorial with the most beneficial concept!

  • @burakuren5188
    @burakuren5188 3 года назад

    i would just say "PERFECT" for this explanation .
    Thanks Tim !!

  • @marctorsoc8309
    @marctorsoc8309 3 года назад

    this is the 5th video I watched today about asyncio and finally got it. Thanks for this, it was really clear :)

  • @jairajsahgal7101
    @jairajsahgal7101 10 месяцев назад

    Thank you so much. I finally understand little bit of asyncio and coroutines.

  • @hasanqadir402
    @hasanqadir402 2 года назад

    Really a conclusive video on asynchronous programming in Python!

  • @MandolinSashaank
    @MandolinSashaank 3 года назад

    Man you make things so simple. Why are you not famous yet! Thank you so much for this! :D

  • @tayyab8215
    @tayyab8215 Год назад +1

    exellent bro! you are the only one❤

  • @MuhammadUsman-yi8bl
    @MuhammadUsman-yi8bl 10 месяцев назад

    thanks man for making these amazing and easy to understand tutorials. keep it comming.

  • @michaelgwinn2571
    @michaelgwinn2571 3 года назад +1

    Amazing video! I think this was my first like video that I’ve watched related to a topic in programming that’s reallly broken it down and I’ve understood it well

  • @adwaith-rajesh
    @adwaith-rajesh 3 года назад +1

    I've been waiting for this video for months ❤️❤️❤️❤️

  • @CookieMonsteeerrr
    @CookieMonsteeerrr 3 года назад

    best explanation ever - including example and explanation. good work

  • @jjophoven
    @jjophoven 2 года назад +1

    THANK YOU!!!!!

  • @vishvajeetramanuj9450
    @vishvajeetramanuj9450 2 года назад +1

    Awesome work dude
    Thank you

  • @lesmonsaluta
    @lesmonsaluta 11 месяцев назад

    Really great video! One of the best ways of explaining I have seen.

  • @balubalaji9956
    @balubalaji9956 2 года назад

    25mins just passed liked that. so well explained.
    thanks

  • @jorge1869
    @jorge1869 2 года назад

    This guy is amazing. Respect

  • @timermens350
    @timermens350 2 года назад

    So clear about all informatiion. Tnx!

  • @markmilan57
    @markmilan57 Год назад

    Nicely explained such a complicated topic master tim. It's a good tutorial.

  • @GelsYT
    @GelsYT 2 года назад

    AT FIRST I DIDN'T UNDERSTOOD BUT THE 2ND TIME IT WAS SO CLEAR! THANK YOU!!!!

  • @angelcaru
    @angelcaru 3 года назад +12

    This was suggested by Dovid S. on this same video, but I would really like a series where you go through the code of popular Python packages and/or modules .

  • @pony_1024
    @pony_1024 3 года назад

    I paused the video and tried the code.... and finally I figured it ! thx !!

  • @yato3335
    @yato3335 3 года назад

    Before this video I knew how threads worked but I was afraid of async, await stuff. Now I understand it better, thank you!

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 3 года назад +4

    12:58 There is a chance that could fail. asyncio only holds weak references to the tasks it creates; it relies on the caller (you) to maintain strong references, to stop those tasks from disappearing randomly. Here it executes after the “task” variable holding the reference has gone.

  • @rotichbill637
    @rotichbill637 3 года назад +1

    Wow great introduction into asyncio. Thanks!!

  • @addmin000
    @addmin000 Год назад

    Well explained. Thanks for the very good intro to asyncio!

  • @deandassa9228
    @deandassa9228 10 дней назад

    great lesson, thank you!

  • @anoriginalnick
    @anoriginalnick 2 года назад

    Excellent demo & explainations!

  • @RiskyRiskForRiskers
    @RiskyRiskForRiskers 3 года назад +1

    THIS VIDEO IS AWESOME. THANK YOU SO MUCH FOR SHARING THIS WITH US.

  • @azr_sd
    @azr_sd 3 года назад

    very simply explained! Very well done Tim.

  • @emiliochaparro8401
    @emiliochaparro8401 3 года назад +1

    Yess! I was waiting for this for a long time

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 3 года назад

    Thanks a lot for your detailed explanation. It really helped me solving one problem related to the sending mail notification.

  • @kawwedha4006
    @kawwedha4006 3 года назад +2

    nice topic chosen wisely 👌🏻, I actually understood you about 70 percent but something is better than nothing.

  • @m.jamilrahman4971
    @m.jamilrahman4971 Год назад

    Since I found out that Python supports asyncio, I always avoid it, I prefer multi threading. But thanks to this tutorial I will start my project using asyncio. Multi threading feat asyncio is the best duet. Great Job Tim, greeting from Indonesia 🍻

  • @moiattube
    @moiattube 2 года назад

    Pretty clear! Nice examples. Thanks a lot

  • @jenex5608
    @jenex5608 3 года назад +5

    Before i was confused with threading and async. Thanks to tim, i now see the difference between threading and async. Threading suspends the thread/task, and runs the other thread, whereas, asycnio doesnt suspend anything it just gives execution

  • @lamtruong6344
    @lamtruong6344 2 года назад

    Great video. It make me realize some thing about acyncIO

  • @mahesh_kok
    @mahesh_kok 2 года назад

    you gave the crux of the async
    now i can finally learn FASTAPI
    thanks a ton buddy

  • @ortaltab5898
    @ortaltab5898 2 года назад

    Thanks Tim! Great video

  • @nsgprominers5223
    @nsgprominers5223 2 года назад

    SUper clear. Thanks!!

  • @JJ-pn8lb
    @JJ-pn8lb 3 года назад +1

    Thank you, very nice video

  • @suchaluch5615
    @suchaluch5615 8 месяцев назад

    As always a great video!
    There is just one point that was missing for me (a python-noob):
    Global variables are shared between the async routines, so it looks to me like is multithreading and not multitasking - that makes it even better!

  • @Nightcrawler333
    @Nightcrawler333 2 года назад

    Thank you, Tim. This video saved me a lot of time..

  • @Stevesteacher
    @Stevesteacher 3 года назад +1

    I love how easy it is to use await, I was expecting it to be more difficult

  • @thisaintmyrealname1
    @thisaintmyrealname1 2 года назад

    Thank you!! It was hard for me to grasp all this just from the asyncio documentation

  • @jjaviergalvez
    @jjaviergalvez 3 года назад

    Terrific job! Finally, I start understanding this topic :)

  • @deemon710
    @deemon710 2 года назад

    This was a good video to get a basic understanding of it but not full enough to actually know what applications to use it for. It reminded me of threads or background jobs.

  • @ajaykryadav
    @ajaykryadav Год назад

    Too good Tim.. 🎉

  • @hosseinfirouzgan
    @hosseinfirouzgan 2 года назад

    Thank you so much. Effective and simple♥♥

  • @JTech0305
    @JTech0305 2 года назад

    thank you it helps me understand asyncio

  • @KyleTennison-v1q
    @KyleTennison-v1q Год назад

    ive been trying to learn this for so long lmfao u made it so easy

  • @okoeroo
    @okoeroo 2 года назад

    Best simple explain, thanks

  • @wannabedev2452
    @wannabedev2452 3 года назад

    I love this man.... exactly what i needed

  • @alonspitz4369
    @alonspitz4369 2 года назад

    Excellent tutorial, thanks.

  • @GreenfieldPortfolioResearch
    @GreenfieldPortfolioResearch 2 года назад

    wonderful class! thank you so much!

  • @gigabytechanz9646
    @gigabytechanz9646 3 года назад

    Really interesting! Good Explanation!!

  • @luisgustavopecanha5507
    @luisgustavopecanha5507 2 года назад

    Great video, man ! Helped me alot :)

  • @karundawadi876
    @karundawadi876 2 года назад

    Thank you for making it so easy to follow.

  • @a.for.arun_
    @a.for.arun_ 2 года назад

    Great explanation!

  • @richardbenes9
    @richardbenes9 3 года назад

    Great. 25 minutes of a content worth investing the time. Thanks. Hopefully, I'll be now able to use this knowledge together with sockets & Tk.

  • @armandomenchaca5653
    @armandomenchaca5653 2 года назад

    Thank you, great explanation!'

  • @yefriencarnacion8717
    @yefriencarnacion8717 3 года назад

    Thank you so much! I could understand this clearly 🤯

  • @Lskm99
    @Lskm99 2 года назад

    very helpful intro, cheers