ID EN
Contextlib

aclosing

Python 3.11

Return an async context manager that calls the aclose() method of thing upon completion of the block. This is basically equivalent to:

Syntax

PYTHON
contextlib.aclosing(thing)

Examples

Example 1
PYTHON
from contextlib import asynccontextmanager

@asynccontextmanager
async def aclosing(thing):
    try:
        yield thing
    finally:
        await thing.aclose()
Example 2
PYTHON
from contextlib import aclosing

async with aclosing(my_generator()) as values:
    async for value in values:
        if value == 42:
            break

See Also

break