Async Techniques and Examples in Python Transcripts
Chapter: Built on asyncio
Lecture: Demo: unsync app introduction

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Time to write some code with Unsync and see how this simple, 126-line package is going to add some incredible power and a really wonderful API
0:10 right on top of everything we've learned. And it's actually going to bring a lot of what we've worked on already together so it's great.
0:17 Now let's begin by looking at the application we're going to write code for. Nothing we're going to do in this video
0:22 has anything to do with unsync yet. We're going to do that in the next video. Here we have three files one called nosync, and that means
0:29 it has no parallelism whatsoever one called presync and that one is going to use pure asyncio and then there's this thesync.
0:38 It starts out using the same as presync but we're going to convert it to the unsync API. What we're going to do in this one
0:45 is we're just going to call these functions and we have three, sorry four categories of functions here. We have one called compute_some.
0:52 If we go to compute_some you can see it's doing math and it prints computing at the beginning, that's cool. So this would be a CPU-bound operation.
1:00 Quick quiz, what is the best type of parallelism to apply to this one? asyncio, threading, or multiprocessing?
1:08 Multiprocessing, right, to do anything CPU-based. Now we're going to download_some we're going to download_some_more. Let's go look at those two.
1:15 In download_some, we're using requests and in download_some_more, we're using this. Now what we're going to imagine here
1:21 is that when we convert it over to the presync one we'll see that for one of these we're going to imagine that we don't have an API
1:30 that is async-enabled but it does HTTP requests under the hood and it's internally using requests. We won't actually be able to await it
1:38 and it won't really take advantage of its powers when we use it with asyncio. The other one will have, actually
1:44 aiohttp as its client and will be properly awaitable. So one of these would be better done with threads
1:50 and the other would be better done with asyncio. Alright, now finally, we're just going to wait and this just puts our thread a sleep for a while
1:58 one millisecond a thousand times, roughly one second. And again, we could easily do this with asyncio we've seen asyncio.sleep
2:06 and we can await that, that one would be perfect. So this first one we're going to run has no parallelism and let's just see how that goes.
2:16 It's computing, computing, computing alright, it's downloading, and downloading more then downloading and downloading more
2:23 it does that a bunch of times. Now it's waiting, now it's done. How long did it take? 9.62 seconds, alright, we'll freeze that
2:30 so we can come back to it. Let's look at the presync one now. All we've done is basically taken as much advantage of asyncio as possible
2:38 so if you look at the functions down here we have an async method computing but because it's computing we can't really do anything but wait
2:46 there's no awaitability here. Async is not quite the proper API, as we'll see. Now this one, this download_some actually is, you know, is internally
2:56 like, you got to use your imagination right internally this part actually uses aiohttp client and we can then, you know, surface its async abilities
3:04 and this is a proper async option so downloadings have happened great. But this one is the one we're imagining
3:12 does not have an async API so threading would help it but asyncio noticed there's no await keyword here actually there's no help, alright?
3:19 This is just as if we called it in serial. Finally, waiting, that one we used asyncio.sleep this one's good. So let's run this one
3:26 and we'll see some things get better like download_some, and wait_some but compute_some and download_some_more
3:33 those, no good. Alright, that'd be a no benefit. So we got computing, see, just as slow. Downloading a little bit better
3:42 waiting, way better, so it's an improvement. Notice it's about twice as fast from 9.62 to 4.93 seconds.
3:53 Alright, so that's what we're going to start with. We have this program we're going to start with this presync mode
3:59 which, alright, okay we're going to pick one of the three APIs to use. Let's pick asyncio. And that is probably the best one
4:06 for the parts it took advantage of but it is not the best one for, say the compute one, or the requests based one.
4:13 So we're going to see that with the unsync API we're going to simplify how this works and we're going to apply the right type of parallelism
4:20 at the right place each right time.

Talk Python's Mastodon Michael Kennedy's Mastodon