Async Techniques and Examples in Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Mixed-mode parallelism
0:00 Once we covered all of the core built-in
0:02 foundational concepts in Python for parallelism
0:05 we branched out and said, well let's look at some extra
0:07 libraries that let us do other things.
0:09 The first one of those was unsync
0:12 and the primary problem that unsync is trying to solve
0:15 really has to do with this mixed-mode parallelism.
0:18 Here we have a bunch of different operations, compute_some
0:21 download_some_more, download_some, and a wait_some.
0:24 The red one, compute_some, is CPU-bound
0:27 that means it should be run in multiprocessing, but
0:30 download_some_more and wait_some, those are asyncio-based.
0:33 They would be bet and most efficiently run on an asyncio loop.
0:37 download_some is implemented in a way that does not support
0:41 asyncio, but it does interact with the the network so
0:43 threading actually would be really perfect for it.
0:45 How do we run all those together?
0:47 Well, it's means different APIs if we use
0:50 all the foundational building blocks.
0:52 But with unsync all you have to do is call these functions
0:56 and you can wait on the results in a block
0:58 that basically waits for the work to be done.
1:00 And you specify through the unsync decorator
1:03 and the method signature what type of method that it is
1:07 and then unsync decides how to run it best.
1:09 So the first one, we say it's unsynced but CPU-bound.
1:13 Unsync says, "Great, we're using multiprocessing."
1:15 The next one is a regular function so def
1:18 not async def, just def, and so, unsync looks at that
1:21 and says, "Well, okay, here's a regular function
1:23 we can't run it on asyncio, so let's run it on a thread."
1:27 Finally, at the end we have an unsync, async method
1:30 so async def, and that means this one does support asyncio
1:34 so let's run it on an ambient asyncio event loop
1:37 on a background thread that we don't have
1:39 to think about or worry about.
1:40 All of that happens in just this little bit of code, here
1:44 and it's beautiful.
1:45 What's also nice is unsync is only 126 lines of code
1:48 in its entire implementation
1:50 so it's not some huge, massive library
1:52 it just is a really clean API on top of
1:55 all the foundational stuff we've already spoken about.