On this page
contextlib.aclosing
Prev: contextlib.AbstractContextManager | Up: contextlib | Next: contextlib.asynccontextmanager
Rel: contextlib.AbstractAsyncContextManager, contextlib.AbstractContextManager, contextlib.asynccontextmanager, contextlib.AsyncExitStack
Ref: docs.python.org — contextlib.aclosing
Tags: #public #python3 #stdlib #contextlib
Import
import contextlib
from contextlib import aclosing
Signature & Overview
contextlib.aclosing(thing)
Async context manager for safely finalizing an asynchronously cleaned-up resource such as an async generator, calling its aclose() method.
Usage
import asyncio
from contextlib import aclosing
async def ticker():
for i in range(5):
yield i
async def main():
async with aclosing(ticker()) as gen:
first = await gen.__anext__()
assert first == 0
asyncio.run(main())
Gotchas
- Always wrap partially consumed async generators: Guarantees
await gen.aclose()runs immediately on exit instead of waiting for garbage collection. - Requires Python 3.10+: Async counterpart to
contextlib.closing().
REPL Inspect
from contextlib import aclosing
help(aclosing)
Linked from
- tuts/python3/stdlib_module/contextlib
- tuts/python3/stdlib_module/contextlib/AbstractAsyncContextManager
- tuts/python3/stdlib_module/contextlib/AbstractContextManager
- 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/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/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