Python 3, an illustrated tour Transcripts
Chapter: Asynchronous Programming
Lecture: Walk-through: Asyncio
0:00 Test, open that up and your editor and see if we can get it going.
0:03 So the first thing you're going to have to do is use pip to install pytest asyncio
0:09 this is a pytest plugin to enable asynchronous testing.
0:14 Let's run my test here and see if this works.
0:16 So I'm going to hit run asyncio test.
0:22 I have not installed asyncio test so I can do that either from PyCharm
0:27 or I can do that from the command line here.
0:30 I'm going to do it from the command line.
0:32 I'm in the directory where my virtual environment is created on my machine
0:36 and I'm just going to say bin pip install pytest asyncio
0:45 and that should go out and get that and install it.
0:48 So that's how we would install from the command line
0:51 making sure that I'm running pip from my virtual environment.
0:56 Go back and run this guy again and let's see if he passes that guy.
1:01 Okay, now we have installed pytest asyncio,
1:04 let's do the next part here.
1:06 Write a coroutine add2 that accepts two parameters
1:09 adds them and calls asyncio.sleep(0) finally it returns the sum.
1:14 So in order to make a coroutine, we need to say async def
1:19 and it's going to be called add2,
1:22 it's going to take two parameters X and Y
1:25 so it says it's going to add them.
1:27 So I'm going to say result is equal to X plus Y
1:29 and then it says it wants us to call asyncio sleep
1:33 so asyncio.sleep(0) because this is an async call here,
1:39 we can't just cut like this, we need to say await asyncio sleep
1:43 and I have an issue here, it says I need to import asyncio.
1:48 So let's say import that and then let's return the result here
1:52 again the benefit of calling asyncio sleep in a coroutine
1:56 is it gives the event loop the opportunity to run something else.
1:59 So a single coroutine can't hog the CPU
2:05 run this and make sure that it works.
2:09 Okay, so it looks like the add2 part is working.
2:13 Next part says write a coroutine avg that takes two parameters
2:18 coroutines and size, it loops over coroutines and gets the values from them
2:22 when it has pulled out size values, it returns the average of those values
2:27 and says make sure you put in await call in it.
2:30 If you want a well-behaving coroutine you need to put an await call in it.
2:34 So we need to say async, because we're making a coroutine,
2:37 def avg and it's going to have some coroutines.
2:40 So maybe I say cos and I'm going to say size,
2:45 after I get values from those, I'm going to average them.
2:49 So I'm going to loop over my corotines for co in cos,
2:54 I need to accumulate the results of those.
2:58 So I'm going to say result is equal to an empty list.
3:00 I'm going to say res.append await co
3:04 I'm going to wait on that coroutine in my list of coroutines.
3:07 Again when you call it coroutine, you need to say await on it.
3:11 And if the length of res is equal to size,
3:16 then I'm going to say return the sum of res divided by the length of res
3:24 and that should give me the average of the first size coroutine results,
3:31 presumably those are giving me numerical values.
3:40 And when I run that it appears to work.
3:42 So again, key here is you want to have that await in there
3:45 so that you can give the event loop the ability to call another coroutine
3:52 if it wants to and do some switching between them.