Async for loops in Python

preview_player
Показать описание
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.

SUPPORT ME ⭐
---------------------------------------------------
Sign up on Patreon to get your donor role and early access to videos!

Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.)

Want to donate crypto? Check out the rest of my supported donations on my website!

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

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
Рекомендации по теме
Комментарии
Автор

Those flashbacks... Relatable, James.

AntonioZL
Автор

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.

jakedewey
Автор

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

masenius
Автор

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

senpos
Автор

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

etcher
Автор

Thanks for a great video!
Got here after reading about iterators and generators in Fluent Python. This video helps to understand why the Iterator protocol itself is designed the way it is.

andrey
Автор

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

Sylarleft
Автор

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 !

leloubil
Автор

Absolutely fantastic video. Love it. And I'll follow your advice on storing arbitrarily large files and hashing usernames. 😄😁

nspire
Автор

you're soooo freakin' good at explaining things!

spaceyfounder
Автор

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

vamseekotha
Автор

The red logging info feels like bad ux design to me

justincooper
Автор

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.

kocraft
Автор

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

jan.kowalski
Автор

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 :-)

JakubYTb
Автор

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

Hobolover
Автор

This sounds like a sequel to Buckshot Roulette for your foot

AsgerJon
Автор

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 :')

Protocycle
Автор

You also can be an IT humor department consultant.

gencurrent
Автор

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.

davidmurphy