Python 3.11: A Guided Tour Through Code Transcripts
Chapter: Concurrency Improvements
Lecture: AsyncIO Timeout Context Manager

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here's a quick one. We'll just look at the docks. A time out context manager. So whenever you're talking to external systems that might time out,
0:11 I'm waiting for the database to respond, I put something in the queue,
0:15 I'm waiting for a response to be put into another queue after it's been processed. How long do you have to wait for that? Usually not too long.
0:23 Sometimes a long time, potentially forever walking up your system,
0:27 which is not ideal. So one of the additions we get is the asyncio.timeout context manager
0:34 So in this case if we have some asynchronous task here represented by the long running task,
0:41 async method And we want to run it for only 10 seconds and any longer than that becomes an error We just say async with async IO time out 10.
0:51 Put that into the context manager and either it's going to come out successfully or there's going to be an exception
0:57 and it will be canceled. This will show up normally as a cancelation error or canceled error but the context
1:04 manager will transform all canceled errors into time out errors,
1:09 which you can handle as a special case. So there's a specific exception you're going to be able to handle
1:14 that says the thing you try to do timed out through this async time out mechanism.
1:20 Pretty cool. Check it out if that sounds useful. Here is the real easy way to do it in python 311.


Talk Python's Mastodon Michael Kennedy's Mastodon