© Mohamed Mustapha Tahrioui 2019
M. M. Tahriouiasyncio Recipeshttps://doi.org/10.1007/978-1-4842-4401-2_5

5. Working with Async Context Manager

Mohamed Mustapha Tahrioui1 
Darmstadt, Hessen, Germany

Context managers provide a convenient API to govern a runtime context. They expose the ability to hook into entering the context manager scope and exit it. Given that asyncio extends the language with the possibility of execution suspension, it becomes evident that a synchronous context manager cannot interface with the event loop in a seamless fashion.

An asynchronous context manager is a context manager that can use the await keyword to suspend execution in its enter and exit methods. This way, it can yield control back to the event loop and interact ...

Get asyncio Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.