Python 3, an Illustrated Tour Transcripts
Chapter: Asynchronous Programming
Lecture: Walk-through: Asyncio

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Test, open that up and your editor and see if we can get it going. So the first thing you're going to have to do is use pip to install pytest asyncio
0:10 this is a pytest plugin to enable asynchronous testing. Let's run my test here and see if this works. So I'm going to hit run asyncio test.
0:23 I have not installed asyncio test so I can do that either from PyCharm or I can do that from the command line here.
0:31 I'm going to do it from the command line. I'm in the directory where my virtual environment is created on my machine
0:37 and I'm just going to say bin pip install pytest asyncio and that should go out and get that and install it.
0:49 So that's how we would install from the command line making sure that I'm running pip from my virtual environment.
0:57 Go back and run this guy again and let's see if he passes that guy. Okay, now we have installed pytest asyncio, let's do the next part here.
1:07 Write a coroutine add2 that accepts two parameters adds them and calls asyncio.sleep(0) finally it returns the sum.
1:15 So in order to make a coroutine, we need to say async def and it's going to be called add2, it's going to take two parameters X and Y
1:26 so it says it's going to add them. So I'm going to say result is equal to X plus Y and then it says it wants us to call asyncio sleep
1:34 so asyncio.sleep(0) because this is an async call here, we can't just cut like this, we need to say await asyncio sleep
1:44 and I have an issue here, it says I need to import asyncio. So let's say import that and then let's return the result here
1:53 again the benefit of calling asyncio sleep in a coroutine is it gives the event loop the opportunity to run something else.
2:00 So a single coroutine can't hog the CPU run this and make sure that it works. Okay, so it looks like the add2 part is working.
2:14 Next part says write a coroutine avg that takes two parameters coroutines and size, it loops over coroutines and gets the values from them
2:23 when it has pulled out size values, it returns the average of those values and says make sure you put in await call in it.
2:31 If you want a well-behaving coroutine you need to put an await call in it. So we need to say async, because we're making a coroutine,
2:38 def avg and it's going to have some coroutines. So maybe I say cos and I'm going to say size, after I get values from those, I'm going to average them.
2:50 So I'm going to loop over my corotines for co in cos, I need to accumulate the results of those. So I'm going to say result is equal to an empty list.
3:01 I'm going to say res.append await co I'm going to wait on that coroutine in my list of coroutines.
3:08 Again when you call it coroutine, you need to say await on it. And if the length of res is equal to size,
3:17 then I'm going to say return the sum of res divided by the length of res and that should give me the average of the first size coroutine results,
3:32 presumably those are giving me numerical values. And when I run that it appears to work. So again, key here is you want to have that await in there
3:46 so that you can give the event loop the ability to call another coroutine if it wants to and do some switching between them.


Talk Python's Mastodon Michael Kennedy's Mastodon