Async for loops in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ค. 2024
  • Await for it!
    A deep dive into the purpose of the async for loop in Python. How to use the async for loop, why to use the async for loop, and how to write your own async iterators using both async generators as well as by learning the async iterator protocols and writing your own async iterator. We'll see an example ASGI app that computes the SHA256 hash of an uploaded file using Starlette and uvicorn, and write a test client using httpx. Then we implement a simple rate limiter.
    ― mCoding with James Murphy (mcoding.io)
    Source code: github.com/mCodingLLC/VideosS...
    Starlette: github.com/encode/starlette
    FastAPI: github.com/tiangolo/fastapi
    Httpx: github.com/encode/httpx
    Uvicorn: github.com/encode/uvicorn
    SUPPORT ME ⭐
    ---------------------------------------------------
    Sign up on Patreon to get your donor role and early access to videos!
    / mcoding
    Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)
    www.paypal.com/donate/?hosted...
    Want to donate crypto? Check out the rest of my supported donations on my website!
    mcoding.io/donate
    Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information, Pi
    BE ACTIVE IN MY COMMUNITY 😄
    ---------------------------------------------------
    Discord: / discord
    Github: github.com/mCodingLLC/
    Reddit: / mcoding
    Facebook: / james.mcoding
    CHAPTERS
    ---------------------------------------------------
    0:00 Intro
    0:36 Async for syntax and purpose
    2:17 Async web app
    8:30 Async generators
    11:01 Async iterator protocols
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @AntonioZL
    @AntonioZL หลายเดือนก่อน +76

    Those flashbacks... Relatable, James.

    • @kuhluhOG
      @kuhluhOG หลายเดือนก่อน +1

      "like the user name instead"
      I can't even imagine how fun that debugging session was

  • @masenius
    @masenius หลายเดือนก่อน +47

    It would be very interesting with a video explaining how async/await works behind the scenes in python.

    • @user-vt9bp2ei1w
      @user-vt9bp2ei1w หลายเดือนก่อน +4

      This is not mentioned in many teachings. The __await__ magic method is not mentioned in python's async/await teaching, so I have to read PEP 492 to understand what python is doing behind the scenes.

    • @marckiezeender
      @marckiezeender หลายเดือนก่อน +1

      It's generators all the way down

    • @skarfie123
      @skarfie123 หลายเดือนก่อน +3

      Theres a great video by David Beazley called "Build Your Own Async" and a great series by EdgeDB called "import asyncio"

    • @masenius
      @masenius หลายเดือนก่อน

      @@skarfie123 need to check it out, thanks!

    • @marckiezeender
      @marckiezeender หลายเดือนก่อน +1

      @@user-vt9bp2ei1w Yeah, although the best way to define __await__ on your own class is to make it an async method. i.e.
      async def __await__(self):
      return await self.future

  • @jakedewey3686
    @jakedewey3686 หลายเดือนก่อน +33

    Great video as always! I feel like something that could've been touched on is exactly why there's a different syntax for asynchronous for loops. For anyone wondering why, consider what the alternatives for awaiting the next value of an iterator would look like.
    "for x in await y" already has a use: awaiting on y to get a normal iterator, then looping over it synchronously.
    "for await x in y" breaks the normal syntax of for loops, and looks like you're awaiting on the object that the iterator is yielding, instead of awaiting on the iterator itself.
    Maybe something like "for x in async y" would work, but it seems less clear than just labelling the loop as async at the beginning of the line.

  • @senpos
    @senpos หลายเดือนก่อน +11

    Love to Tom Christie, Kludex and other maintainers of starlette, uvicorn and httpx!

  • @justincooper2241
    @justincooper2241 หลายเดือนก่อน +6

    The red logging info feels like bad ux design to me

  • @Sylarleft
    @Sylarleft หลายเดือนก่อน +4

    That's an exceptional video, thanks! I'll keep it in mind for future reference

  • @etcher6841
    @etcher6841 หลายเดือนก่อน +7

    This was a great video. Thank you for featuring Starlette !

    • @MrMeszaros
      @MrMeszaros หลายเดือนก่อน +1

      And uvicorn!

  • @leloubil
    @leloubil หลายเดือนก่อน +2

    You are such a great teacher ! I hope to be as clear as you are when I'll try to be a computer science teacher !

  • @diegosorte
    @diegosorte หลายเดือนก่อน +36

    Yes, please, a FastAPI tutorial!

    • @IqweoR
      @IqweoR หลายเดือนก่อน

      It feels like FastAPI is almost the last thing that needs a beginner tutorial, but it certainly doesn't make it worse if there will be more advanced one. Im all in for advanced

  • @greob
    @greob 24 วันที่ผ่านมา

    Awesome video, super clear and easy to grasp. Love it!

  • @spaceyfounder5040
    @spaceyfounder5040 7 วันที่ผ่านมา

    you're soooo freakin' good at explaining things!

  • @kocraft137
    @kocraft137 หลายเดือนก่อน +1

    When it comes to the section of rate limiting in this vido, I would argue that it is better to calculate the delta time, then check if it is > 0, and if so, call the asyncio.sleep(...). This way you don't call the function. It should not make a notisable difference, but I still think such things are worth thinking about.

  • @Jakub1989YTb
    @Jakub1989YTb หลายเดือนก่อน +7

    Do you know about the view mode "Presentation mode" in pycharm? It is pretty neat to make videos with.
    Also I really liked this video. I'm already awaiting the next one :-)

  • @jan.kowalski
    @jan.kowalski หลายเดือนก่อน

    It would be beneficial to see timings and performance benefits of different approaches.

  • @MarthinusBosman
    @MarthinusBosman หลายเดือนก่อน

    Fantastic video!

  • @Septumsempra8818
    @Septumsempra8818 หลายเดือนก่อน +3

    Concurrent database uploads? How do we upload data that comes in concurrently (eg from a concurrent scraping). All the tuts write to CSV not a db, that's suboptimal for somecases.
    Any advise on writing data concurrently?

  • @Hobolover57
    @Hobolover57 หลายเดือนก่อน +7

    lol I would totally be the coworker that hashes the username. sorry

  • @AsgerJon
    @AsgerJon หลายเดือนก่อน +2

    This sounds like a sequel to Buckshot Roulette for your foot

  • @fancypants6062
    @fancypants6062 หลายเดือนก่อน +1

    i love this channel

  • @jvcrules
    @jvcrules หลายเดือนก่อน

    What are your thoughts on alternative async libraries, like trio?

  • @ValidatingUsername
    @ValidatingUsername หลายเดือนก่อน +2

    Can we async the Dyson sphere construction?

    • @mCoding
      @mCoding  หลายเดือนก่อน +2

      We can built it, we have the technology

  • @0xDEAD_Inside
    @0xDEAD_Inside หลายเดือนก่อน

    Can async for be used to read from socket connections? I am struggling to find any code snippets for this.

  • @gencurrent
    @gencurrent หลายเดือนก่อน +1

    You also can be an IT humor department consultant.

  • @Protocycle
    @Protocycle หลายเดือนก่อน

    really love how Python can be so modular without making it too different from other similar modules... but I'm still too dumb to understand it all :')

  • @marcinp3343
    @marcinp3343 หลายเดือนก่อน

    gimme that tutorial mate! In a series!

  • @dupdrop
    @dupdrop หลายเดือนก่อน +2

    Your examples do `asyncio.sleep(0.0)` when `max_sleep_duration` is surpassed. It seems that it's not exactly the same as a nop, similar to how `time.sleep(0.0)` triggers a context switch. Might be worth to explore.

    • @marckiezeender
      @marckiezeender หลายเดือนก่อน +1

      asyncio.sleep(0) basically allows asyncio to check its event queue. So if there are other tasks that need attention then asyncio will switch to those contexts. Once those tasks sleep (or wait for IO, or complete), asyncio will switch back to the first task.

    • @skillfulfighter23
      @skillfulfighter23 หลายเดือนก่อน

      @@marckiezeenderso it's basically the same as a yield?

    • @marckiezeender
      @marckiezeender หลายเดือนก่อน

      @@skillfulfighter23 Yeah, it's the async-await equivalent of a bare yield.

  • @kitanaigaijin
    @kitanaigaijin หลายเดือนก่อน +1

    Please include oauth in fastapi!

  • @dirtdart81
    @dirtdart81 หลายเดือนก่อน

    I grabbed the code from the github to play around with it and it didn't know what "Iterable" or "Awaitable" was until I added the imports. On a quick search I wasn't able to find that it is a new python feature, how did you get away without importing?

    • @mCoding
      @mCoding  หลายเดือนก่อน +3

      Fixed, thank you! I didn't include all my local changes in the commit I pushed.

  • @pietraderdetective8953
    @pietraderdetective8953 หลายเดือนก่อน +3

    Quart need some spotlight...it is the async version of Flask. Migrated to it last year and got zero issues so far. Another plus: the most important Flask extensions are working well with Quart. Can you please make a video on Quart?

    • @aflous
      @aflous หลายเดือนก่อน +1

      Did you try Dash with Quart?

    • @pietraderdetective8953
      @pietraderdetective8953 หลายเดือนก่อน +1

      @@aflous no it was for a webapp i'm building..initially was using Flask but then it couldn't handle concurrent users, I was really close to upgrading servers. Migrated to Quart, postponed the upgrade due to how well async Quart handles concurrency.

    • @aflous
      @aflous หลายเดือนก่อน

      @@pietraderdetective8953 I see. Wondering if it is any better than fastapi though? (which is using starlette under the hood)

  • @davidmurphy563
    @davidmurphy563 หลายเดือนก่อน +24

    I wrote a function the last day and I was ten minutes trying to work out why it wasn't compiling to bytecode.
    I forgot you need to write "def"... I lost the next ten minutes laughing.

  • @vamseekotha
    @vamseekotha หลายเดือนก่อน +13

    I am gonna say something that will make the comment section jealous. I met James in person !

  • @TimGrob
    @TimGrob หลายเดือนก่อน +2

    Yes, could make a fastAPI tutorial

  • @lahirusandaruwan6124
    @lahirusandaruwan6124 8 วันที่ผ่านมา

    🙏

  • @NMiller_
    @NMiller_ หลายเดือนก่อน

    I see "Big Encode" got to you....

  • @qexat
    @qexat หลายเดือนก่อน +2

    discord gang

    • @mCoding
      @mCoding  หลายเดือนก่อน +1

      Thanks as always!

  • @realedna
    @realedna หลายเดือนก่อน +1

    I'm pretty sure your code doesn't really provide a proper form of rate-limiting any API-calls, but rather only presents the results/awaitables "rate-limited".
    Usually rate-limiting can be implemented with a decorator, that defers the actual call to a function with a wait till the next free time slot. So a new task will only be created and allowed to run after some waiting.
    With your code every awaitable is most likely an already running task and could be executed to completion with enough awaits happening somewhere in the code.

    • @realedna
      @realedna หลายเดือนก่อน +1

      So your code works when use_api() is a coroutine (as shown), but it wouldn't if use_api() were a normal function that creates tasks and returns an awaitable.

  • @smitty683
    @smitty683 หลายเดือนก่อน

    Getting some manor hash slinging hasher vibes here

  • @jotjakubjot
    @jotjakubjot หลายเดือนก่อน +1

    I'm letting you know to make a FastAPI tutorial.

  • @ConstantlyDamaged
    @ConstantlyDamaged หลายเดือนก่อน +2

    _sighs_
    I wish I didn't have to save content. My image sourcing/profiling Discord bot has to download them, though, so it can pHash and examine the image/video's colors.
    So, it is well insulated should it throw exceptions, it has a cleanup task that cleans out old files in case there is something orphaned, and I have the folder things are saved in be its own volume on my VPS where it cannot possibly kill the OS by filling up the main volume.
    All said and done, though, it's nice to have a video on more async stuff. That's my bread and butter (though I typically use aiohttp for my client calls).

    • @undergroundmonorail
      @undergroundmonorail หลายเดือนก่อน

      could you download them into a bytesio object? that's what i've done when i needed file-like objects containing things i've downloaded but didn't want to actually save them to disk

  • @30IYouTube
    @30IYouTube หลายเดือนก่อน +1

    APRIL FOOOOOOOOOOOOLS DAYYY

  • @kexec.
    @kexec. หลายเดือนก่อน

    what is benefit that can get from async loop…?

    • @thunder____
      @thunder____ หลายเดือนก่อน +8

      If you have a lot of data you need to send and/or receive through a network, USB device, or similar, the thing that usually causes the most delay is the network/device connection, rather than the CPU like you'd experience with heavy computations. That means that, for example, if you have an application where you need to download a large number of files from a server, if you don't do it asynchronously, your CPU is going to wait for each download to finish before starting the next one, meaning it sits idle most of the time while waiting for chunks of data to come in through the network connection, which is a big waste of time. Each individual download also probably isn't using up the entire bandwidth of your network connection, meaning even more time is wasted.
      That's the problem async operations (or threading, which is very similar) aims to solve, by allowing your program to start the next download even while the previous one isn't finished yet. That way, the CPU can send out all the download requests at basically the same time and get the full use out of the network connection and as much use as possible out of the CPU.
      Async (and threading) will NOT help with heavy computations. If your program is slow because it's having to do a lot of complex math or anything like that, you'll be better served by multiprocessing (although in that case, it's also probably worth considering using Cython or a C extension or perhaps just using a different language completely, although there are certainly reasons why you might not want or be able to do so, in which case multiprocessing may be very useful for you).

    • @kexec.
      @kexec. หลายเดือนก่อน

      @@thunder____ yeah i usually use async only for non blocking purpose but it’s hard to imagine when async loop is really needed 😢 I mean I need to loop each item all through

  • @Vkr0101
    @Vkr0101 หลายเดือนก่อน

    Please make a fastapi tutorial.

  • @sloan00
    @sloan00 หลายเดือนก่อน +2

    2:29 FastAPI! FastAPI! FastAPI!

  • @oleksiiolchedai7512
    @oleksiiolchedai7512 หลายเดือนก่อน

    Subscribe and w8 4 tutorial! FastAPI

  • @mraviteja22
    @mraviteja22 หลายเดือนก่อน

    FastAPI tutorial please.

  • @FirstNameLastName-fv4eu
    @FirstNameLastName-fv4eu หลายเดือนก่อน

    Come on Man!! there must be an easy way to solve this problem. This is like OverEng in steroid !!

  • @RyanMorey1
    @RyanMorey1 หลายเดือนก่อน +3

    0th

  • @jnorch
    @jnorch หลายเดือนก่อน

    FastAPI tutorial please

  • @Efecretion
    @Efecretion หลายเดือนก่อน +1

    Just my opinion, but the whole async/await ideas in Python and JS are simply hacks to make asynchronous programs look like synchronous programs when writing -- that is, it makes it easier to learn but at the same time hides the details from programmers, thus preventing them from learning and fully understanding what is going on
    In small toy examples like this it can make code look 'cleaner', but in actual complex products it is much better to design the applications as asynchronous _in structure_, not simply in spirit.

    • @thunder____
      @thunder____ หลายเดือนก่อน +2

      I agree, but at the same time, simplifying things such that a programmer doesn't have to learn what's going on under the hood is basically Python's entire mission statement (and I believe explicitly so, but even if not, definitely in practice). I'm personally not a fan of Python due to that fact (as well as due to the lack of static typing and lack of access to manual memory management), but there is great value in a tool that makes it possible for people to get things done without having to become an expert. Some people who aren't primarily software developers have a need to be able to write their own applications without needing to become experts in the field, so Python can be a wonderful tool for those kinds of use cases.
      However, for anyone who is or intends to become a developer as a career, I fully agree that learning how to do threading without this async syntax is very valuable. But at the same time, for those people, I also think learning at least one lower-level language is practically a must, especially with how competitive the job market has gotten recently. Being competent in both Python and C (or at least Go or Kotlin or *shudders* Java) will look a lot better on a resume than just Python (and/or JavaScript and/or PHP).

    • @amath3307
      @amath3307 หลายเดือนก่อน +1

      ​@@thunder____ as far as I understand, threads and async/await, despite both providing concurrency, do not do the same thing. While threads operate by having the CPU rapidly switching between multiple tasks, Python's asyncio is all within a single thread. However, please feel free to correct me if I am wrong.

    • @realedna
      @realedna หลายเดือนก่อน +2

      @@amath3307 Yes, asynchronous is a form concurrency, but has merely the use of preventing waits to block the execution of other tasks and rearrange execution of tasks, if necessary. It isn't a way to run things truly in parallel, but that's what threads are for and you can combine those paradigms.
      Yet, with threading your code usually can be interrupted at any point and requires locks and atomic sections to make it work correctly in any case.
      With async you only interrupt on awaits and some async-API calls, but usually don't have to use locks or atomic sections to make things work. Every code fragment between between two interruptions is already guaranteed to run alone, because of the single execution thread.

    • @thunder____
      @thunder____ หลายเดือนก่อน +1

      @@amath3307 I actually was never sure async and threads did the same thing under the hood, but even though they don't, the functionality is quite similar and fits approximately the same use cases, so I stand by the substance of my comment even if my implication that it was just another way to write the same thing is not quite correct.

    • @Efecretion
      @Efecretion หลายเดือนก่อน +1

      @@thunder____ Great point! I do think that making programming more fun and rewarding for new programmers is great, and in some way Python is more geared towards that then most high profile programming languages.

  • @Alex63285
    @Alex63285 หลายเดือนก่อน

    first?

    • @mCoding
      @mCoding  หลายเดือนก่อน

      Gratz

  • @kutilkol
    @kutilkol หลายเดือนก่อน

    The head is horrid

  • @Mekuso8
    @Mekuso8 หลายเดือนก่อน +28

    Please do not make a FastAPI tutorial. It doesn't need any more attention, and it's honestly not very good. If you'd make a tutorial on Litestar or Starlette though, that would be awesome.

    • @linux_devs
      @linux_devs หลายเดือนก่อน

      I agree with this

    • @MichaelBoratko
      @MichaelBoratko หลายเดือนก่อน +3

      This is so surprising to me, FastAPI is my preferred solution! Why do you consider it not very good?

    • @Mekuso8
      @Mekuso8 หลายเดือนก่อน

      @@MichaelBoratko Contrary to what its name suggests, it's not very fast. Litestar is 10x faster in my application. It is quite bloated and its documentation is somewhat cryptic on certain topics. It's also primarily made by a single developer who seems to have a bit of an ego issue, based on some questionable statements on the FastAPI GitHub page. Litestar feels much more based.

    • @linux_devs
      @linux_devs หลายเดือนก่อน +1

      It's not that Fast API is bad. It's that Fast API is built on top of Starlette. It also doesn't get the love it should.

    • @saadatbaig
      @saadatbaig หลายเดือนก่อน +1

      I'd actually pitch a sanic tutorial or at least a showcase of sanic to write (asynchronous) web applications.
      (imo) it's very pleasant to use if you're coming from / know Flask and has a thorough documentation 🙂