Python 3, an Illustrated Tour Transcripts
Chapter: Asynchronous Programming
Lecture: Coroutine Requirements
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about what's found in a coroutine. Again, you need to declare a coroutine with async before the def
0:06 any non blocking code, you need to call await before it in Python 3.6 and again, this allows you to do that context switch
0:14 from one green thread to another you can't call await in functions, they can only be called in coroutines. anything that has an async def.
0:22 If you want to return a value, there's a couple ways that you can return a value we can pass in this future that we saw and just say future.set result,
0:29 we can also return from a coroutine. And then when we say run until complete we will get a future as a result and we can call result on that
0:40 to pull off the result of what's returned. So those are a couple ways to return a value. Typically, I find myself using futures because it seems for me
0:49 to make it a little bit more clear because you need to pack all these things into the event loop and typically you have multiple things running
0:56 and so my code seems to have futures rather than just returning the result from the gathered future from every coroutine that I'm collecting together.
1:06 These coroutines need to be put into the event loop and so to put them into the event loop we can say asyncio wait
1:12 or asyncio gather or we can just pass in a coroutine directly into that run until complete wait and gather allow us to take multiple coroutines
1:21 and make a new future out of those to pass into our loop. Let's look at the interface for future and what we can do with the future.
1:30 So there are a couple ways to create futures. You can call the constructor, but I'd advise against that
1:35 rather I would call, if you're creating a future loop create future. This allows you if you're using an alternate event loop,
1:43 they can create their own future that has an alternate implementation rather than hard coding it to the asyncio future that's in there.
1:49 So there could be some optimizations that alternate event loops that you could plug in as using, but this is the interface for futures
1:55 once we have a future we can call await on it and that waits until result arrives, we can call set result on it, we can call set exception on it,
2:03 we can have a call back on it. We can pass in a function and that function will be called with the future when the callback is done.
2:11 We can call exception to get the returned exception and we can call result to get the returned result.
2:17 Note that if the future does not yet have a result, this will raise an invalid state error.
2:22 It could also return a cancelled error if the future was canceled. So typically, we don't like go into a loop and call if f result and try
2:30 and see if there's an error there rather we use this code right here, result is equal to yield from f and that will give us our result.
2:39 We don't have to go into a loop or anything. If we want to cancel our future, we can say cancel
2:43 and we can get the status of whether it's done or canceled.