What Are Python Coroutines?
This lesson and the subsequent ones introduce Python coroutines and the benefits of running things asynchronously. You’ll learn what they are and how they compare to generators.
In this video, you’ll also see how to create a synchronous function that prints out a random number after a few seconds.
def randn():
time.sleep(3)
return randint(1, 10)
00:00 The next thing I want to talk about is coroutines. Coroutines are similar to generators, in that they can be stopped and paused and restarted, but they’re different in the sense that generators generate values and then coroutines consume values.
00:16 So you can think of generators as producers and coroutines as more like consumers of data, okay? I think the best way to show this is just through an example.
00:28
I’m going to create a function that will give me a random number, say, between 1
and 10
, after 1 second. Okay. Now, I’m going to show the synchronous version of this and then the asynchronous version. And after I compare or juxtapose the two, hopefully you’ll get a little bit more idea of the power of asynchrony.
00:49
Okay. So I need to import a few packages up here at the top of my file. I’m going to say from random import randint
. This will give me a random integer. Next thing I want to do is import time
.
01:11
This is so I can time some things so you can see the real value of this asynchronous stuff, if you time it. And then the next thing I want to do is import asyncio
.
01:26 Like that. So, those are the three imports. Next thing I want to do is create my function that generates random numbers. And like I said, I’m going to do this synchronously and then asynchronously.
01:37
The synchronous version would look something like this. def randn()
for some random number, and I would do time.sleep()
, let’s say for 3
seconds.
01:51
And then I would just return randint()
, 1
to 10
. Okay. So this is not a coroutine, this is a normal function. I’m going to sleep for 3
seconds and then I’m going to emit out some random number. Let’s go to IPython and try this thing out.
02:13
So I’m going to type in ipython -i theory.py
,
02:20
whos
, and there’s my randn
function. So let’s just run this thing. So if I say randn()
—remember, it gives me a random number between 1
and 10
. And it did—it gave me 8
. But how long did it take? Well, I can do %time
,
02:41
and %time
shows me that it took 3
seconds to run and the output was a 2
. But what if I wanted to run this thing multiple times and then see how long it took? So, you see it took 3
seconds to run it one time, but what if I wanted to run it three times? Well, that’d be 3 * 3, so you would imagine it would take 9 seconds, and let’s just try that.
03:02
Let’s create a little list comprehension here. I say [randn() for i in range(3)]
. I don’t really need the i
, we can just make this an underscore (_
). And let’s time that.
03:18
Now, you may think it was 3
seconds per, and that I’m doing this three times—3 * 3 is 9—and let’s see if that is actually the result. And yeah. These are the three values I got back from the function, and yes—it did indeed take 9 seconds.
Become a Member to join the conversation.
UBBA on May 16, 2019
Note that iPython is now Jupyter. %time will work in Jupyter which can be run right inside Visual Studio Code(Run python in interactive window).