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