On this page
contextlib.AbstractAsyncContextManager
Up: contextlib | Next: contextlib.AbstractContextManager
Rel: contextlib.AbstractContextManager, contextlib.aclosing, contextlib.asynccontextmanager, contextlib.AsyncExitStack
Ref: docs.python.org — contextlib.AbstractAsyncContextManager
Tags: #public #python3 #stdlib #contextlib
Import
import contextlib
from contextlib import AbstractAsyncContextManager
Signature & Overview
contextlib.AbstractAsyncContextManager()
An abstract base class for asynchronous context managers.
Usage
import asyncio
from contextlib import AbstractAsyncContextManager
class AsyncSession(AbstractAsyncContextManager):
async def __aenter__(self):
return "session-open"
async def __aexit__(self, exc_type, exc, tb):
return False
async def main():
async with AsyncSession() as s:
assert s == "session-open"
asyncio.run(main())
Gotchas
- Provides default
__aenter__returningself: Only__aexit__is abstract, making custom async context managers concise. - For simple cases, prefer
@asynccontextmanager: A generator function is shorter unless your resource needs methods or state.
REPL Inspect
from contextlib import AbstractAsyncContextManager
help(AbstractAsyncContextManager)
Linked from
- tuts/python3/stdlib_module/contextlib
- tuts/python3/stdlib_module/contextlib/AbstractContextManager
- tuts/python3/stdlib_module/contextlib/aclosing
- tuts/python3/stdlib_module/contextlib/asynccontextmanager
- tuts/python3/stdlib_module/contextlib/AsyncExitStack
- tuts/python3/stdlib_module/contextlib/chdir
- tuts/python3/stdlib_module/contextlib/closing
- tuts/python3/stdlib_module/contextlib/ContextDecorator
- tuts/python3/stdlib_module/contextlib/contextmanager
- tuts/python3/stdlib_module/contextlib/ExitStack
- tuts/python3/stdlib_module/contextlib/nullcontext
- tuts/python3/stdlib_module/contextlib/redirect_stderr
- tuts/python3/stdlib_module/contextlib/redirect_stdout
- tuts/python3/stdlib_module/contextlib/suppress
Related notes
- 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/AsyncExitStack 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/chdir links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/closing links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ContextDecorator links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/contextmanager links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ExitStack links to this page; shared tags: #contextlib, #stdlib