Async Techniques and Examples in Python Transcripts
Chapter: Why async?
Lecture: Python's async landscape

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I want to draw a map or an illustration for you of Python's Async landscape. So you'll see that there are many
0:07 different techniques that we're going to employ throughout this course and different techniques apply in different situations or they will potentially
0:14 give you different benefits. So we're going to break the world into two halves here. On one, we're going to say "Can we do more at once?"
0:23 That's exactly the thing we just spoke about with say the web server. While our database request is busy waiting
0:29 let's go do something else. The part we opened this chapter with about leveraging the CPU cores and actually computing
0:36 a single thing faster by taking advantage of the mutli-core systems, that's doing things faster. Over in the do more at once, we have this thing called
0:45 Asyncio. Asyncio was introduced to Python around Python 3.4, but really came into it's own when the language began to work with it, with async and
0:55 await keywords in Python 3.5. So Asyncio is the easiest, clearest, simplest way to add scalability like we just saw in our concurrent request
1:06 example. You're waiting on something, go do something else. We also have threads and threads have been around much
1:12 longer. They're harder to coordinate, they're harder to deal with error handling, but they're also a really great option
1:19 to do more at once. In some programming languages, threads would also be over in the do things faster. We're going to
1:27 get to why that is, but you may have heard a thing called the GIL and the GIL means computationally threads are
1:32 effectively useless to us. They let you do more things at once if there's a waiting period, but if it's purely
1:38 computational in Python, threads are not going to help us. What will help us? Because the GIL is a process level thing, Python has this
1:47 capability to spawn sub-processes and a lot infrastructure and APIs to manage that. So instead of using threads, we
1:55 might kick off five sub-processes that are each given a piece of the data and then compute the answer and return it to the main process.
2:02 So we'll see multi-processing is a tried and true way to work with adding computational performance improvements
2:10 and taking advantage of multiple cores and this is usually done for computational reasons. We want to leverage the
2:17 cores, not we want to talk to the database a bunch of times in parallel. We would probably use threads for that.
2:24 We're also going to look at C and Cython. C, C++, and Cython. So C obviously can do multiple multi-threaded stuff. It could also do more at once
2:33 but you know, we're going to leverage it more for the aspect of doing things faster in a couple ways.
2:40 However, writing C can be a little bit tricky. It can be error prone and so on. As a Python developer, it would be
2:47 better if more of our code could actually be in Python or very very close to Python. So we're going to talk about this thing called Cython
2:56 not CPython but Cython. Cython is a static compiler that compiles certain flavors of Python to C and then basically compiles that C
3:07 and runs it under the restrictions that C has which are very limited rather than the restrictions that Python has which is fairly limited.
3:15 So we'll be able to use Cython for very powerful multi-threading as well. That's more using the threads in a computational way.
3:23 So this is the landscape. On one hand we have do more at once. Take advantage of wait periods. On the other, we have do things faster
3:31 take advantage of more cores. Now these are all fine, Asyncio is really nice, but there is also other libraries and other techniques out there that
3:40 allow us to do these things more easily. We're going to look at two libraries. These are by no means
3:47 a complete list of these types of libraries, but we're going to look at something called Trio something called Unsync. Aynsc Unsync - get the play
3:55 on words right? So these are higher level libraries that do things with the Asyncio capabilities, with threads
4:02 with multi-processing and so on, but put a new programming API on them, unify them, things like that.
4:10 This is your Python Async landscape. Do more at once do things faster, do either of those easier.
4:18 Basically, that's what this course is about. By the end of this course, you'll have all of these in your tool box
4:23 and you'll know when to use which tool.

Talk Python's Mastodon Michael Kennedy's Mastodon