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

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


Talk Python's Mastodon Michael Kennedy's Mastodon