Python tricks: Demystifying async, await, and asyncio

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 237

  • @cakeray
    @cakeray 6 ปีที่แล้ว +194

    This is one of the best asyncio explanations if come across so far. Kudos!

  • @ondskabenselv
    @ondskabenselv 3 ปีที่แล้ว +1

    This is _the best_ asyncio tutorial I've yet encountered.
    Succinct while not dumming the explanations down!

  • @anumsheraz4625
    @anumsheraz4625 5 ปีที่แล้ว +41

    man who are you ? why I didn't knew you before !
    Never seen such way of explanation in the way you did. AMAZING, Thank you !

  • @victortarnovskiy8407
    @victortarnovskiy8407 6 ปีที่แล้ว +17

    Probably the clearest example of all I saw trying to figure out the way async/await works. Combined with some theory from PyCon presenters (on matters like "what is event loop for") this one completes the puzzle. Thank you so much for your efforts!

  • @jimmy2643
    @jimmy2643 3 ปีที่แล้ว +6

    This is by far the best simplistic explanation i've seen! Major props and thank you for the video.

  • @shopifyexplained
    @shopifyexplained 6 ปีที่แล้ว +2

    You know yours is by far the best explaination of Async I have seen on the internet over the past two days trying to learn how async works.

  • @Dtomper
    @Dtomper 3 ปีที่แล้ว +4

    You are sooooooo underrated man, I don't know what's wrong with the TH-cam algorithm not showing your videos to people....

  • @Covalent5
    @Covalent5 4 ปีที่แล้ว +2

    Your teaching capabilities are beyond everything i've encountered.

  • @abhijeetkislay8920
    @abhijeetkislay8920 4 ปีที่แล้ว +2

    You sir have cleared such deep rooted doubts that I had for parallelism in Python for years and years! Thanks a lot!

  • @cetrusbr
    @cetrusbr 4 ปีที่แล้ว +1

    This video is certainly the most helpful about this subject by far

  • @thabangtsajoa716
    @thabangtsajoa716 3 ปีที่แล้ว

    I have tried reading around async all over TH-cam,
    even if you were a IT teacher at junior schools, 10 year olds kids would understand you because of how you simplify it. Thank you Sebastiaan

  • @jamtart22
    @jamtart22 3 ปีที่แล้ว +2

    In case anyone is interested - to determine if a number is prime you only need to check if it has a prime factor smaller than or equal to its square root. Because C=AB must have factor A

  • @KhoiThinh
    @KhoiThinh 5 ปีที่แล้ว +1

    This is hands down the best explanation about asyncio.

  • @letslearnabout4934
    @letslearnabout4934 5 ปีที่แล้ว +1

    Very great explanations. Not only I know how to use asyncio, but also how it works. Thanks!

  • @alexeysilver3139
    @alexeysilver3139 5 ปีที่แล้ว +10

    Man, this is the best explanation ever, thank you

  • @Lorant1984
    @Lorant1984 2 ปีที่แล้ว +1

    Oh man, thanks for that. I am yet to understand the details yet, but after watching your video I understood the "concept" of asynchrony and the role of the event loop. GREAT explanation!

  • @rajeshkab
    @rajeshkab 5 ปีที่แล้ว +1

    I have been trying to understand this concept from past 1 month.. Now I feel much better watching this video.. Thanks Sebastiaan

  • @amitbuch
    @amitbuch 4 ปีที่แล้ว +2

    Loved this short tutorial. You are real guru. Salute.

  • @AlexHerlan
    @AlexHerlan 6 ปีที่แล้ว +11

    you did a much better job explaining this compared to the top 4 articles returned by google on the subject. Thank you. I like your gradual approach to implementing it... really helped a lot. I'm a lot more familiar with asynchronous javascript, which this is a bit different from... but I think I like it. I certainly like Python over JavaScript in general.

    • @thedaynos
      @thedaynos 5 ปีที่แล้ว +1

      I agree. I have been banging my head against my desk the past few days trying to figure this out from the garbage google links. Just watching this on break at work and i think I can figure out the issue I'm dealing with tonight no problem. thank you!

    • @davidegrandis3716
      @davidegrandis3716 4 ปีที่แล้ว +1

      Totally agreed, by far the best explanation

  • @JoseRodriguez-go5do
    @JoseRodriguez-go5do 5 ปีที่แล้ว +2

    Great explanation with the live coding, really helps to get the concept, but as you said, I/O operations like network, file access and other stuff that depends on "outside code" is what really makes it shine

  • @rongeorgia3937
    @rongeorgia3937 6 ปีที่แล้ว +3

    First, an excellent presentation and explanation. My sincerest thanks. Second, when I first saw the "talking" head in the corner I was like, "What the...?" But as the video went on it was like I was sitting in a coffee shop looking over his shoulder as he explained what was going on. So, I am sold on this. Great job Sebastiaan.

  • @saitaro
    @saitaro 7 ปีที่แล้ว +60

    These glasses are fundamental, mate. Thanks for the lesson. btw, could you please explain the practical difference between async, threading, processes etc.?

    •  7 ปีที่แล้ว +38

      Thanks! The difference between threading, multiprocessing, and async is a video on its own, but here's the quick summary: With *multiprocessing you* simply start your program twice; so you have two separate instances, each with their own variables, etc. As you can image, communication between processes is difficult. With *threading*, you start two threads of code that run in parallel within a single process (so they share variables, et.c). Unlike with async, the operating system decides which thread runs when. And *async*, as explained here, is in a sense a way to simulate threading in a more controlled way that gives you (almost) complete control over how functions suspend and resume.

    • @UniBreakfast
      @UniBreakfast 7 ปีที่แล้ว +3

      Any real life examples when this is applicable?

    •  7 ปีที่แล้ว +8

      +Михаил Нинин the stereotypical use case would be network communication, in which you often need to wait for data to come in. But I've used coroutines also to implement asynchronous display control and collection of keyboard input.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 6 ปีที่แล้ว +5

      Any situation where you need to do multiple things at once, but your code is not CPU bound. In such a situation, threads buy you nothing but complications, whereas coroutines are much simpler to deal with.

    • @AlibekAydarov
      @AlibekAydarov 6 ปีที่แล้ว +2

      th-cam.com/video/9zinZmE3Ogk/w-d-xo.html - watch this, very good explanation of their core differences

  • @yahya_abou_imran
    @yahya_abou_imran 6 ปีที่แล้ว +1

    I would like to thank you because it is very difficult to find such a simple example on the net at this time...
    At the same time, to teach how to make your code async incrementally is just wonderfull.
    Amazing video!
    Highly recommended for beginners in async stuff like me!

  • @antonyspiano2403
    @antonyspiano2403 4 ปีที่แล้ว +1

    Thanks brother! can't find anything simple than this in terms of explanation basic concepts.
    Crystal clear, Thank you!

  • @alexandrutoma9187
    @alexandrutoma9187 4 ปีที่แล้ว +2

    This is the best coroutines tutorial on the internet.

  • @AmitTiwari-sb3qy
    @AmitTiwari-sb3qy 3 ปีที่แล้ว +1

    Sebastain, Great Video, I have one doubt, what if => await asyncio.sleep() is not used inside asyc code. Right now I am dealing with some async code. Here code is using async because we have to wait to get some json data from server but I have not seen anywhere , we have used something like await asyncio.sleep(). So does our code is fully asynchronous or do we get benefit by introducing await asyncio.sleep() in our async code

  • @andreypanin5257
    @andreypanin5257 6 ปีที่แล้ว +7

    This was extremely useful and well explained. There's a lot of docs on this subject, but none of them tell how the transition of execution to the loop occurs. Or maybe it's just me who couldn't understand?

  • @waelsadek81
    @waelsadek81 5 ปีที่แล้ว +2

    Thanks for the video well explained. For the is_prime function you can do the division check to and including the square root only no need to go past that.

  • @qipress9552
    @qipress9552 3 ปีที่แล้ว

    I am trying to wrap my head around async/await, threading and multiprocessing in Python. This demo and some of your statements you made were very helpful. I need a few clarifications:
    1) For the example that was discussed, because there was an asynchronous sleep, while that was "in progress", it gave up control to the underlying event loop, which in turn switched to the next call, which after doing some quick CPU load (of running the loop) again went to asynchronous sleep, and so on. This meant that "most" of the time, all three sleeps were in progress simultaneously, thus giving the illusion of parallelism. If instead of the asynchronous sleep, if I chose to run a compute intensive calculation (like increment i to a billion), that would be blocking, and would no longer be async, correct?
    2) So the await HAS to be on a load that is OUTSIDE the CPU - sleep, network, read disk, etc., correct?
    3) Is there a way for me to write my own code which can simulate an async load outside the current thread/process? Like I really do want to increment from 1 to a large number then return, but should happen parallely over multiple coroutines (if that is the right word). Maybe call an external Python script which does that (which will be another process perhaps, I am not sure).

  • @ashukarappa
    @ashukarappa 4 ปีที่แล้ว +3

    Awesome explanation. This was a really great video explaining the concept clearly and in a step by step thought process manner!! Really loved the video. Great job!

  • @ripperx444
    @ripperx444 6 ปีที่แล้ว

    Finally i understand this better now. People need to do what you did which is explain why you use the keywords and how that works!

    •  6 ปีที่แล้ว

      Glad you found it useful!

  • @admin202
    @admin202 4 ปีที่แล้ว +1

    explanation skills at peak

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 6 ปีที่แล้ว +2

    Note that asyncio is not just a standard Python event loop, it is the standard Python event loop API. There are already plenty of event loops around--in particular, every GUI toolkit already provides one. Rather than try to force everybody to adopt the asyncio event loop (which would never work anyway), what you need to do is wrap these other event loops in an asyncio-compatible layer. Then it becomes possible to write “event-loop-agnostic” code, which is something I don’t think any language has achieved before.
    For example, GTK has its own event loop, provided by GLib. Here github.com/ldo/glibcoro is an asyncio-compatible wrapper for it, implemented in a little over 400 lines of code. This makes it possible to use the GLib event loop interchangeably with the default asyncio one in non-GUI apps. In GUI apps that are built on GTK, it becomes possible to use Python coroutines to run large parts of the application logic, instead of the more usual event callbacks. Both scenarios are demonstrated here: github.com/ldo/glibcoro_examples

  • @Thompsonnumber4
    @Thompsonnumber4 4 ปีที่แล้ว +1

    Clear and concise, easy to follow. Excellent!

  • @RulerOfCakes
    @RulerOfCakes 3 ปีที่แล้ว

    The best explanation I've come by so far. Thank you so much for this video.

  • @kenrosenberg8835
    @kenrosenberg8835 3 ปีที่แล้ว

    This tutorial is so on point, the best one on python asyncio

  • @shervinledu9440
    @shervinledu9440 4 ปีที่แล้ว

    one-liner for is_prime function at 5:25 is just so nice ! :D

  • @zhaofengzheng2923
    @zhaofengzheng2923 4 ปีที่แล้ว +1

    You are the coolest programmer I have seen :)

  • @yandelyano
    @yandelyano 4 ปีที่แล้ว +1

    Subscribed shortly after the video started.

  • @donha475
    @donha475 5 ปีที่แล้ว +4

    That was brilliant! The best explanation I've seen! Well done mate! ;)

  • @uhadonejob
    @uhadonejob 4 ปีที่แล้ว +1

    Brilliant explanation. Next level teaching.

  • @MuhammedBasil
    @MuhammedBasil 4 ปีที่แล้ว

    Very comprehensive... Very well explained. Thanks mate.

  • @abdallahabdillah8477
    @abdallahabdillah8477 4 ปีที่แล้ว

    i don't know why people would dislike this video!!

  • @ingenzivany6566
    @ingenzivany6566 4 ปีที่แล้ว +1

    The best aSYNCIO explanation video thank you.

  • @Mankind5490
    @Mankind5490 5 ปีที่แล้ว +1

    You're the hero we got, but didn't deserve.

  • @VIKASHKUMAR-kx6vy
    @VIKASHKUMAR-kx6vy 3 ปีที่แล้ว

    Watched two times And now I think I understand it well.

  • @Jagnathbaba
    @Jagnathbaba 6 ปีที่แล้ว +1

    very good explanation. One thing I wanted to ask. The time that you give in asyncio.wait function, is that even useful? I mean, we know that it's a point where the function is suspended and another functions might be called. But once another function is called, we cannot really be sure that the function will suspend and get back to the original function in the specified time?

  • @codebasics
    @codebasics 3 ปีที่แล้ว +4

    Very well explained, love your explanation using code :)

  • @girishsancheti9523
    @girishsancheti9523 7 ปีที่แล้ว +14

    Very well explained!! Hats Off :)

  • @AlexBenfica
    @AlexBenfica 5 ปีที่แล้ว +1

    The best explanation so far! Nice style!

  • @Nakameguro97
    @Nakameguro97 4 ปีที่แล้ว +1

    Excellent! Explains why I can't use requests directly in code with coroutines (they block). Subscribing.

  • @fleecky7011
    @fleecky7011 2 ปีที่แล้ว +1

    Hele duidelijke tutorial, dankjewel!

    •  2 ปีที่แล้ว

      Graag gedaan!

  • @toastrecon
    @toastrecon 6 ปีที่แล้ว +5

    Awesome. Thanks for posting this. The example was great.

  • @Regalman
    @Regalman 4 ปีที่แล้ว +1

    Nice shades and so clear!

  • @fygarOnTheRun
    @fygarOnTheRun 5 ปีที่แล้ว +1

    Short and concise. What else do you need? Dank je well!

  • @99icd
    @99icd 2 ปีที่แล้ว +1

    Top ! a very clear explanation. Well done and thank you.

  • @gennadyplyushchev1465
    @gennadyplyushchev1465 3 ปีที่แล้ว

    Finally understandable explanation! Thank you!

  • @asadraja377
    @asadraja377 7 ปีที่แล้ว +2

    Very informative and beautifully presented. Thank you so much!

  • @manoskoutoulakis6398
    @manoskoutoulakis6398 5 ปีที่แล้ว +1

    Best asyncio explanation 👍👍👍👍👍

  • @RogerValor
    @RogerValor 6 ปีที่แล้ว +1

    well explained, took it as a refresher. Would be awesome to have advanced look at Future in a follow up.

  • @brambeer5591
    @brambeer5591 4 ปีที่แล้ว +2

    Great explanation fellow Dutchman!

  • @MuhammedBasil
    @MuhammedBasil 4 ปีที่แล้ว +1

    @Sebastiaan
    if we move `await asyncio.sleep(0.01)` above the for loop, loop execution will be happen asynchronously, right ?
    async def highest_prime_below(x):
    print('Highest prime below %d' % x)
    await asyncio.sleep(0.01)
    for y in range(x-1, 0, -1):
    if is_prime(y):
    print('→ Highest prime below %d is %d' % (x, y))
    return y
    return None

  • @ELHAUKEZ
    @ELHAUKEZ 5 ปีที่แล้ว +1

    Hey Sebastian, what do you think about a follow up video for asyncio with generators for the suspending part. With a great example and the quality of your other videos that'll be a nice ressource. So long, best wishes

  • @yourdadsbestfriend7101
    @yourdadsbestfriend7101 3 ปีที่แล้ว +1

    so underrated! amazing

  • @HrachyaArshakyan
    @HrachyaArshakyan 6 ปีที่แล้ว

    Only human friendly explanation that I able to find over internet

  • @МихайлоСвєчкін
    @МихайлоСвєчкін 3 ปีที่แล้ว +1

    This is a great explanation! Thanks!

  • @abhishekguha1931
    @abhishekguha1931 7 ปีที่แล้ว +1

    Excellent ! Understood very well from your video. Keep on the good work.

  • @RazmikPoghosyan
    @RazmikPoghosyan 3 ปีที่แล้ว

    HI Sebastiaan, thanks for amazing explanation. BTW, I have a way of determining of primariness of number. I would like you to take a look at it.
    def is_prime(x):
    return not any (not x % i for i in range(2, int(x ** 0.5)))

  • @finster101
    @finster101 4 ปีที่แล้ว

    Excellent video! Very clear and understandable. Thanks for sharing your knowledge.

  • @marcioinfoful
    @marcioinfoful 5 ปีที่แล้ว +4

    Thanks, Dude! that help me a lot! A really great explanation!

  • @bierviltbever
    @bierviltbever 4 ปีที่แล้ว

    Now this guy is on my wavelength

  • @gnsc
    @gnsc 5 ปีที่แล้ว +1

    very nice explanation with live coding

  • @fredpaik1349
    @fredpaik1349 5 ปีที่แล้ว +1

    Great explanation. You got my subscription and like for this video. Thanks Sebastiaan!

  • @mohnnishsaravanan
    @mohnnishsaravanan 4 ปีที่แล้ว +1

    man ! u r the best ! thanks a lot for this best explanation .

  • @stegallo
    @stegallo 5 ปีที่แล้ว

    Thanks for the video! Very useful. In your example you call all your functions at the same time. What if you want to call them (assume all of them are independent functions) at different times? Do you still need to have them in the wait list? Like appending them at the time of call?

  • @nix1994pd
    @nix1994pd 3 ปีที่แล้ว +1

    Beautiful. Thank you man!

  • @AnimeshPaul23
    @AnimeshPaul23 4 ปีที่แล้ว +1

    Amazing explanation. Thank you Sir. :)

  • @jorgekleinerman6682
    @jorgekleinerman6682 6 ปีที่แล้ว +2

    Excellent explanation!! Congrats and thank you very much!

  • @ButchCassidyAndSundanceKid
    @ButchCassidyAndSundanceKid 4 ปีที่แล้ว +1

    Why did you wear sunglasses indoors ?

  • @beckhamwilson2112
    @beckhamwilson2112 2 ปีที่แล้ว +1

    great video thanks also those are some cool glasses

  • @Matteinko
    @Matteinko 6 ปีที่แล้ว +1

    nice lesson; please continue to make some!

  • @arifbasri4950
    @arifbasri4950 5 ปีที่แล้ว +2

    This is a great explanation, thank you for this video and the code

  • @JoseGarcia-kq2pg
    @JoseGarcia-kq2pg 4 ปีที่แล้ว +1

    Thanks for the great explanation.

  • @DmytroLituiev
    @DmytroLituiev 7 ปีที่แล้ว +2

    great explanation. can you provide your notebook?

    •  7 ปีที่แล้ว

      Thanks! And sure, here it is: osf.io/w8u26/

  • @dluxdoggdlux
    @dluxdoggdlux 6 ปีที่แล้ว +1

    Hey bro, really good stuff. Keep the shades on.

  • @adityamathur2093
    @adityamathur2093 3 ปีที่แล้ว +2

    This guy is a freakin ROCKSTAR! Keep going man

  • @aiminzeng9310
    @aiminzeng9310 6 ปีที่แล้ว +1

    Good. I almost understood. I am Chinese with not very good English. Thanks a lot!

  • @Albert-fe8jx
    @Albert-fe8jx 6 ปีที่แล้ว +1

    Very clear. Excellent. Thank you SM.

  • @MrRocksW
    @MrRocksW 5 ปีที่แล้ว +1

    Thanks, great video!

  • @NinKillPokemon
    @NinKillPokemon 4 ปีที่แล้ว +2

    Great video!

  • @Griimnak
    @Griimnak 6 ปีที่แล้ว

    Well explained and you definitely know what you're talking
    I watched another tutorial where the guy was mixing return and loop.create_task(), idk if he knew what he was talking about.

  • @evgenykuznetsov7490
    @evgenykuznetsov7490 4 ปีที่แล้ว +1

    this is a great explanation.

  • @elachichai
    @elachichai 3 ปีที่แล้ว

    Great video. Why didnt you use async def is_prime ? Overlooked? Does not matter?

  • @AbhishekNigam
    @AbhishekNigam 5 ปีที่แล้ว +1

    Excellent explanation!!

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 6 ปีที่แล้ว

    8:00 The asyncio library is pure Python. That means that anything the event loop can do, your own Python code can do. You can implement your own event loop!

  • @cryp0g00n4
    @cryp0g00n4 4 ปีที่แล้ว +2

    Wow - this was really good

  • @yuriyprykhodko
    @yuriyprykhodko 4 ปีที่แล้ว +1

    Great example, and a well-paced video, not over-complicated, but still detailed enough!
    And btw, it's `x%i == 0`, not `x//i == x/i` ;)

  • @sohambiswas8865
    @sohambiswas8865 4 ปีที่แล้ว +1

    Thanks for the video man, Loved it

  • @andrez76
    @andrez76 3 ปีที่แล้ว

    Great video, full of interesting insights and information on the mechanics of asynchronous code execution. However, allow me to add that, if execution time is the main factor, using async is pointless for CPU-bound code such as the one used in the example. You may achieve concurrent execution by yielding from your co-routines, but that will not help in total execution time.

    • @Lorant1984
      @Lorant1984 2 ปีที่แล้ว

      This has been acknowledged in the video at 15:02.

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 6 ปีที่แล้ว

    15:15 In that situation you would probably use the select module docs.python.org/3/library/select.html to manage concurrent connections and get the same performance. The convenience of coroutines is being able to write out your logic as a linear sequence, rather than having to break it up into separate pieces invoked via callbacks.