On this page
contextlib.AsyncExitStack
Prev: contextlib.asynccontextmanager | Up: contextlib | Next: contextlib.chdir
Rel: contextlib.AbstractAsyncContextManager, contextlib.AbstractContextManager, contextlib.aclosing, contextlib.asynccontextmanager
Ref: docs.python.org — contextlib.AsyncExitStack
Tags: #public #python3 #stdlib #contextlib
Import
import contextlib
from contextlib import AsyncExitStack
Signature & Overview
contextlib.AsyncExitStack()
Async context manager for dynamic management of a stack of exit callbacks.
Methods & Attributes
AsyncExitStack.aclose(self)— Immediately unwind the context stack.AsyncExitStack.callback(self, callback, /, *args, **kwds)— Registers an arbitrary callback and arguments.AsyncExitStack.enter_async_context(self, cm)— Enters the supplied async context manager.AsyncExitStack.enter_context(self, cm)— Enters the supplied context manager.AsyncExitStack.pop_all(self)— Preserve the context stack by transferring it to a new instance.AsyncExitStack.push(self, exit)— Registers a callback with the standard exit method signature.AsyncExitStack.push_async_callback(self, callback, /, *args, **kwds)— Registers an arbitrary coroutine function and arguments.AsyncExitStack.push_async_exit(self, exit)— Registers a coroutine function with the standard aexit method signature.
Usage
import asyncio
from contextlib import AsyncExitStack, asynccontextmanager
@asynccontextmanager
async def tag(name: str, log: list[str]):
log.append(f"+{name}")
try:
yield name
finally:
log.append(f"-{name}")
async def main():
log: list[str] = []
async with AsyncExitStack() as stack:
await stack.enter_async_context(tag("a", log))
await stack.enter_async_context(tag("b", log))
assert log == ["+a", "+b", "-b", "-a"]
asyncio.run(main())
Gotchas
- LIFO teardown: Contexts and callbacks registered with
enter_async_context()/push_async_callback()unwind in reverse order, even if an error occurs partway through. - Handles both sync and async managers: You can also call
stack.enter_context()for regular synchronous context managers on the same stack.
REPL Inspect
from contextlib import AsyncExitStack
help(AsyncExitStack)
Linked from
- tuts/python3/stdlib_module/contextlib
- tuts/python3/stdlib_module/contextlib/AbstractAsyncContextManager
- tuts/python3/stdlib_module/contextlib/AbstractContextManager
- tuts/python3/stdlib_module/contextlib/aclosing
- tuts/python3/stdlib_module/contextlib/asynccontextmanager
- tuts/python3/stdlib_module/contextlib/chdir
Related notes
- tuts/python3/stdlib_module/contextlib/AbstractAsyncContextManager Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/AbstractContextManager Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/aclosing Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/asynccontextmanager Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/chdir Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/contextlib/closing shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ContextDecorator shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/contextmanager shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ExitStack shared tags: #contextlib, #stdlib