Python 3, an Illustrated Tour Transcripts
Chapter: Asynchronous Programming
Lecture: Walk-through: Asyncio Context Managers
0:00 In this video, we're going to look at async context test.py
0:04 Let's look at the first test, it says write an asynchronous context manager staller,
0:08 it should accept a time in seconds when the context is entered,
0:12 it should pause for that many seconds, call asyncio sleep.
0:15 Okay, so let's make a staller.
0:18 We have to define a class called staller
0:20 and we need to define a constructor __init__
0:25 and it needs to take some time in seconds
0:28 and because this is an import there rather than saying time,
0:34 maybe I'll just say amount and I'll say self.amount is equal to amount.
0:38 Now we need to define the implementation for the asynchronous protocol
0:42 of the context manager and that is defining __aenter__ and __aexit__
0:49 and because this is asynchronous,
0:51 we're going to put an async right in front of that def there.
0:55 Okay, it says when we enter we want to call asyncio.sleep
1:01 and we want to sleep for self.amt
1:04 and we're getting squiggles here in PyCharm
1:06 because we need to import asyncio there
1:10 and because we're calling an asynchronous function,
1:13 remember, whenever we call asynchronous functions, we need to await them.
1:16 So this is a function that can call asynchronous functions
1:22 because it is defined with async in front of it.
1:24 Great, we just also need to define the async def __aexit__
1:30 and this doesn't need to do anything, so we'll just pass here.
1:34 Let's run this and make sure that it works,
1:36 so I'll just right-click and say run this.
1:38 And it looks like I'm on the second part here,
1:42 so this is my staller context manager,
1:45 it takes an amount that we want to stall
1:48 and when we enter the context it will sleep for that amount before starting the context.
1:53 We can look down at the test here just to make sure that that's what's going on.
1:56 We have a time, we keep track of the time before we go into it,
2:00 we're going to say we're going to stall for one second
2:02 and then we're going to enter it, we're going to look at our time
2:05 and we're going to assert that our time difference
2:08 is greater than or equal to 1 second, which it appears to be now.
2:11 The next one says write an asynchronous context manager closer.
2:13 It should accept an object and call the .close method on it when the context exits.
2:19 Okay class closer, we need the constructor,
2:26 and it needs some object here, I'm just going to say obj
2:30 and let's say self.obj is equal to obj,
2:33 and we need a __aenter__, __aexit__ async def __aenter__
2:41 and this doesn't need to do anything here
2:44 and we need a async def __aexit__
2:50 and when we exit we need to make sure that we call closer here
2:54 so you can say self.obj.close.
3:01 Let's run this and make sure it works.
3:04 Okay, it looks like it worked and we can look at the test down here.
3:10 The test just makes a class called CloseMe and if you call close on it,
3:15 it sets an attribute called closed is equal to true
3:17 and inside of here, we just create an instance of CloseMe
3:21 and we put it in our async context manager here
3:24 and we don't do anything, we just assert that after we're done closed is called.
3:30 Note that nowhere inside of this code have we called closed
3:35 but we passed the c object into closer and it called that when it exited out of it.
3:41 Hopefully you have a little bit more understanding
3:44 how to use these asynchronous context managers
3:46 the key point being that if you want to do some asynchronous calling
3:52 you can do that inside of the __enter__ or __exit__ if you want to.
3:57 Note that closer doesn't do any asynchronous calling.
4:00 It just closes an object so you can make the context managers like that
4:05 as well if you want to.