Async Techniques and Examples in Python Transcripts
Chapter: async and await with asyncio
Lecture: Faster asyncio loops with uvloop

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I want to highlight a project that can almost effortlessly make your asynchronous code faster and the project is called uvloop.
0:10 So, uvloop is a reimplementation of the asyncio event loop. And it turns out that the way Python was built
0:20 that asyncio event loop can be replaced with others. And so the async and await keyword can run on different implementations of that IO loop.
0:29 And this happens to be one of them so it's a quick drop-in replacement for the built-in asyncio event loop. It's implemented in Cython, and uses libuv.
0:38 So Cython is basically Python compiled to native optimized C code and libuv is a lower level library
0:48 that is an event loop for different languages built in C. And if you look here, a quick claim here is uvloop makes asyncio 2 to 4 times faster.
0:59 So here you can see built-in socket behavior versus over here, that socket one, that's quite a bit faster. Or protocol versus this protocol
1:08 it's actually a little hard to figure out what goes where but these are all the native standard behavior speeds
1:15 and these are if you just do this quick drop in. So how do we do that? Well, it's not too hard. So let me show you a simple little program
1:29 that I built. It's a little bit fake, but that's OK. So it's going to come on here and it has a limit
1:34 of 250,000 items. I guess we could make it more obvious like this. And it's going to use the async event loop and it's going to run, and run, generate
1:44 generate, and process. And now what we're doing is we're not doing hardly any sleep. We are sleeping a tiny, tiny bit but we're just saying
1:52 "give up our time slice and let something else run but then immediately pick us up, and keep going." And we're doing this 250,000 times.
2:00 So what we're doing is, instead of having a few big blocks that we're slicing our work up into like previously I think we had 20 things we produced
2:10 and 20 things we consumed, and each one had two sort of waiting points, so at most we broke it into 80 slices. And those little slices were executed
2:19 over a period of 21 seconds. Here, we're going to take a quarter million slices executions, twice, actually we're doing this here
2:31 so maybe 4 times that actually. So, what is that, a million times 2 so that's a couple million slices running as fast as we can.
2:38 And when we have things that fine-grained very, very quick and fine-grained then we start to see the behavior
2:45 the actual performance of the underlying asyncio event loop itself come into play. So if you just have a few coarse-grained slices
2:54 this is like, not required, right? But if you're breaking things into vere fine-grained bits that run for very short periods of time
3:00 maybe this technique is going to be helpful for you. So let's run this real quick. We have a standard version and a uvloop version
3:07 and we'll do the uvloop from scratch. So here you can run it with two times the quarter million actions. Now we wait, and it takes 8.7 seconds, OK.
3:19 So that's how long this one took. Let's go and do the work to implement our uvloop version. Now, we're going to have uvloop as a dependency.
3:31 And we're going to need to install that here. I don't think it's installed. Let's go to our terminal and say pip install uvloop.
3:38 Spelling is hard. OK, that worked. Now all we have to do is we have to import uvloop and we have to say asyncio.set_event_loop_policy().
3:53 Event loop policy. So basically this is telling asyncio when you create your event loop use UV's event loop implementation. That's it.
4:04 We haven't had to touch our code or anything. We just have to run that at startup and let's see how things work now.
4:14 4.7 seconds. 8.7, 4.7, and all we did is change the underlying implementation. If this kind of stuff is going to give you a big improvement
4:24 go ahead and take the dependency on uvloop. You're probably depending on other packages anyway. It's really awesome.
4:30 Like I said, if you're breaking it up into very coarse-grained things you know, it probably is not worthwhile. But it's such a easy performance win
4:40 that if it helps you, you should definitely think about using uvloop.


Talk Python's Mastodon Michael Kennedy's Mastodon