Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What Is asyncio?

Give Feedback

This lesson covers what Async IO is and how it’s different from multiprocessing. You’ll see what happens when your code makes responses, how the requests are dispatched to the outside world through the event loop, how responses are handled, and how asyncio ties into all this.

00:00 So, what is asyncio actually doing? Well, let’s juxtapose or compare it to the multiprocessing library. In multiprocessing, what you’re doing is—something is slow, so you’re effectively creating copies. You’re creating multiple processes, so instead of just running your code in one process, you run it in two processes, three processes, four processes.

00:21 And these processes usually run on cores on your CPU, so if you have an 8-core CPU and if you have eight processes, then you could run, say, one process on each CPU.

00:33 In that case, everything would be running at the same time, in parallel.

00:39 But, like I mentioned, if your app is IO-bound, if you’re doing a lot of IO processing, then instead of using multiprocessing what you can do is instead of use asyncio.

00:49 Now, what asyncio does is this: it’s only one process and one thread within one process, so it’s effectively doing just one thing at a time.

00:58 What happens is you’re going to send out a bunch of requests inside your Python code. So, imagine this is your Python code here, and let’s say you write to the file system, write to the file system, query the database, query the database, and make an HTTP request, make another HTTP request—so, these are all sorts of different work requests that you’re going to do from your Python code. But you’re going to do them one at a time. You’re going to say “Do this, now do the next thing, now do the next thing, now do the next thing.” So inside of here, they’re going to run one at a time—sequentially.

01:30 So, they’re going to leave and they’re going to go into this thing called the event loop, and this event loop is inside of Python too. Now, what the event loop is going to do is it’s going to send out these requests—these requests are coming into the event loop—it’s going to send them out to the external world, meaning to the file system. So you could write something to the file system, to some external database, to some external computation—to something external to your Python program. It’s going to send it out, okay?

01:57 So all these things are being sent out. Now, once they’re sent out, they could potentially be running in parallel then. So, they’re out, they’re running, the database is querying some stuff, the file system is writing some stuff, you’re sending some requests to a website, and then these responses come back. Responses—maybe from a file system, maybe you’re reading a file. Maybe the response comes back from a database, maybe you’re querying a database, et cetera.

02:18 So, these responses come back, and once it comes back, then you’re going to get an operation complete from the operating system, and the operating system is going to let Python know, it says, “Hey, this thing is finished.” Then the event loop is going to remember, “Oh, you did five, you sent out five requests.” And so once the response comes back in, then I can take that response and say, “Oh, this response was to the request of”—let’s say you did a request for “Read something from the file system.” Once that was finished, the response comes back from the file system saying, “Hey, this is finished.” And then it triggers what’s called a callback, and the event loop calls into Python. It says, “Hey, Python. This file has now been read,” or “This database query has now been fulfilled,” or “This some sort of external computation is finished.” Then it continues running sequentially in here.

03:05 So, basically, the request gets sent out, it goes into the event loop, the event loop fires it off. It runs in whatever manner necessary—it could be in parallel, it could not be parallel.

03:15 It doesn’t really matter from Python’s perspective. But typically it runs pretty fast. And then once the response comes back, the event loop lets Python know that this thing is finished running and then it continues running.

03:25 But something interesting happens when the request comes out. You say, for example, “Read something from a file.” Well, what Python is going to do is it’s actually going to pause the execution of that function, and that function is called a coroutine, and we’ll be talking about coroutines later, but it pauses the execution of that coroutine until later on when the event loop says, “Hey, this thing is finished!” and then the function, or that coroutine, gets reentered and it resumes execution. So, once again, this sounds a little bit confusing, but I’m going to be showing you some code in a minute and hopefully it’ll be less confusing then. But just once again, as an overview, these requests happen in Python, they happen one at a time, they go into the event loop, the event loop sends out the requests, and then once they come back and—like I said—once the requests are sent out, they could be potentially running actually in parallel.

04:20 So this could be the parallel section here. And then the response comes back and then this event loop notifies Python, “Hey, something has just finished and you may want to continue running.” Okay?

04:31 So, Python runs these things one at a time, but out here in the real world, it could be running in parallel. So that’s what it does. Once again, I think if I show you some of the code for this, it’ll help this gel a little bit. And I would, if I were you, come back to this slide and kind of just like refer to this slide as you’re going through the code, and then maybe it’ll make these two things gel together. Okay. With that, let’s start coding.

Become a Member to join the conversation.