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