On this page
  1. contextlib.aclosing
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from contextlib import aclosing
help(aclosing)

Linked from

Related notes