Learn Python's AsyncIO in 15 minutes

Поделиться
HTML-код
  • Опубликовано: 6 янв 2025

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

  • @WDieSmile
    @WDieSmile Год назад +10

    Simple but most essential and elegant explanations for the asyncio. Huge thanks!

  • @ramima6465
    @ramima6465 10 месяцев назад +2

    The `await asyncio.sleep(0.5)` isn't needed because "the task sometimes happends so fast that task.canceled is not going to be able to register the cancel in enough time"! But it's needed because `task.cancel()` doesn't actually start the cancelation but merely schedule it for cancellation. The task will be cancelled when control returns to the event loop, which happens when an await expression is encountered.

  • @goodmanshawnhuang
    @goodmanshawnhuang 11 дней назад

    Great tutorial, it helps a lot, thank you so much for sharing it, keep posting these wonderful videos!🥰

  • @YoKKJoni
    @YoKKJoni 7 месяцев назад +1

    11:10 the asyncio.sleep can be set to asyncio.sleep(0.0) and it will stil register
    i know its a bit late but still might be relevant..

  • @djbroake9810
    @djbroake9810 Год назад +2

    Thanks for the content. I think there are a few ways to do this, In the example you execute the function and then append the returned data to the list, rather than appending the function call itself to a list, Here are 2 examples:
    tasks = [loop.create_task(kill_time(i)) for i in range(1, 10+1)]
    await asyncio.wait(tasks)
    for task in tasks:
    print(task.result())

    or
    list_of = []
    for i in range(1, 10+1):
    task = asyncio.create_task(kill_time(i))
    list_of.append(task)

    await asyncio.gather(*list_of)

  • @eitantal726
    @eitantal726 7 месяцев назад +1

    1:44 I think the point of 'await' is to let python do a context switch. Normal function call to sleep will also transfer control to the next line after the sleep is done, await doesn't affect that

    • @karola.7908
      @karola.7908 Месяц назад

      Is that how it works in python? In languages like JS, Dart or C# not awaiting an async function is just basically a fire and forget approach, the thread gets spun off to do its thing and the next line executes immediately

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

    Can I use this to constantly check if a program is running, then make an if statement saying if the program is not running open a file and if it’s already running print the name of the current file it’s running? I’m trying to make this but with a start and stop button…

  • @eldebtor6973
    @eldebtor6973 9 месяцев назад +2

    doesnt await defeat the asynchronous behavior and make it blocking? 🤒

    • @eugenmalatov5470
      @eugenmalatov5470 9 месяцев назад +2

      i think the explanation is wrong. Await starts a async coroutine, nothing to do with waiting afaik.

    • @joakker8820
      @joakker8820 6 месяцев назад

      If I understand correctly, if you await, the execution will continue in another coroutine, which could be the one you just created, or another

  • @gauravshaurya
    @gauravshaurya 7 дней назад

    I have seen your face first time in this video. I didn't know, you are that cute from voice. Love your tutorials.

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

    I think this video is great. Very clear.

  • @hillolbro
    @hillolbro Год назад +4

    This is arguably the best asyncio-await explanation on youtube!. thanks for keeping it simple to understand!

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

    @7:15 isnt the function kill_time() already called before you do await.gather()

    • @e_a_r_t_h_m_a_n
      @e_a_r_t_h_m_a_n 11 месяцев назад +1

      coroutines (any function with `yield` or `await` inside) should be `extracted` from a function first by a call syntax. But this call is not a call, it just returns a `coroutine` object.

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

    Very clear 👌

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

    are u using python 3.10?? bcs some function has depreced

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

    Thanks o this video I've decided to rewrite my synchronous data grabber (web scrapping and directly via API) based on requests to asyncio and aiohttp. Speedup was about ~2x :)

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

    👏Clear and concise

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

    Great video, thanks!

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

    This was very helpful. Thank you.

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

    thanks really understood the concept.

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

    Thanks for this exposition 👍

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

    Thanks for a simple explanation!

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

    great tutorial!

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

    For a noob like me this was awesome! Thank you

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

    Awesome vid; glad I stumbled upon this! Much cleaner than threading/mp libraries, so I want to learn this one! Why does main have to be async def main and not just def main? Also, does that mean that any functions I use in the async module must have with the async keyword in the definition?

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

    Thanks for the explanation

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

    11:15 I am strongly against waiting like that. I would loop or create something awaitable instead, but I want to learn it, and you giving me an anti-pattern like that makes me question the usefulness of the rest :/

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

      You can be strongly against whatever you want. The sleep can also be set to 0 if you want, but you need to provide your script some context to be able to perform a switch or it never will.

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

    Nice explanation, very helpfull for me. A huge thanks !!

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

    6:57 No.
    Use list_of_tasks = [kill_time(i) for i in range(1000)]

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

      Why do you need list comprehension versus what he had?

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

    Can you please provide source code for these videos

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

      I don’t have source code for these videos

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

      @@Indently you delete all the example files after writing ?

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

      @@__lasevix_ Yeah, I don't share source code in general for non-project videos

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

      Just follow along in practice. Not everything has to be a handout.

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

    Your voice & accent sounds a lot like Naval Ravikant
    😃

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

    very nice

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

    fuoco frigiteli y adizo

  • @Alex-bw8wr
    @Alex-bw8wr 2 года назад +4

    Not super useful 😢

    • @Indently
      @Indently  2 года назад +6

      Thank you for the useful comment 😉