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