Async Context Manager

Provides an implementation of asynchronous context manager and its applications.

Note

The async context managers in this module are transparent aliases to contextlib.asynccontextmanager of the standard library in Python 3.7 and later.

class AbstractAsyncContextManager[source]

An abstract base class for asynchronous context managers.

AsyncContextManager

alias of contextlib._AsyncGeneratorContextManager

async_ctx_manager(func)

A helper function to ease use of AsyncContextManager.

actxmgr(func)

An alias of async_ctx_manager().

class aclosing(thing)[source]

Async context manager for safely finalizing an asynchronously cleaned-up resource such as an async generator, calling its aclose() method.

Code like this:

async with aclosing(<module>.fetch(<arguments>)) as agen:

<block>

is equivalent to this:

agen = <module>.fetch(<arguments>) try:

<block>

finally:

await agen.aclose()

class closing_async(thing: aiotools.context.T_AsyncClosable)[source]

An analogy to contextlib.closing() for objects defining the close() method as an async function.

New in version 1.5.6.

class AsyncContextGroup(context_managers: Optional[Iterable[contextlib.AbstractAsyncContextManager]] = None)[source]

Merges a group of context managers into a single context manager. Internally it uses asyncio.gather() to execute them with overlapping, to reduce the execution time via asynchrony.

Upon entering, you can get values produced by the entering steps from the passed context managers (those yield-ed) using an as clause of the async with statement.

After exits, you can check if the context managers have finished successfully by ensuring that the return values of exit_states() method are None.

Note

You cannot return values in context managers because they are generators.

If an exception is raised before the yield statement of an async context manager, it is stored at the corresponding manager index in the as-clause variable. Similarly, if an exception is raised after the yield statement of an async context manager, it is stored at the corresponding manager index in the exit_states() return value.

Any exceptions in a specific context manager does not interrupt others; this semantic is same to asyncio.gather()’s when return_exceptions=True. This means that, it is user’s responsibility to check if the returned context values are exceptions or the intended ones inside the context body after entering.

Parameters

context_managers – An iterable of async context managers. If this is None, you may add async context managers one by one using the add() method.

Example:

@aiotools.actxmgr
async def ctx(v):
  yield v + 10

g = aiotools.actxgroup([ctx(1), ctx(2)])

async with g as values:
    assert values[0] == 11
    assert values[1] == 12

rets = g.exit_states()
assert rets[0] is None  # successful shutdown
assert rets[1] is None
add(cm)[source]

TODO: fill description

exit_states()[source]

TODO: fill description

class actxgroup

An alias of AsyncContextGroup.