ID EN
Contextlib

AsyncExitStack

Python 3.11

An asynchronous context manager, similar to ExitStack, that supports combining both synchronous and asynchronous context managers, as well as having coroutines for cleanup logic.

Syntax

PYTHON
class contextlib.AsyncExitStack

Examples

Example 1
PYTHON
async with AsyncExitStack() as stack:
    connections = [await stack.enter_async_context(get_connection())
        for i in range(5)]
     All opened connections will automatically be released at the end of
     the async with statement, even if attempts to open a connection
     later in the list raise an exception.

See Also

asynchronous context manager ExitStack close() aclose() ExitStack.enter_context() TypeError AttributeError ExitStack.push()