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