mode.utils.contexts
¶
Context manager utilities.
-
class
mode.utils.contexts.
AbstractAsyncContextManager
[source]¶ An abstract base class for asynchronous context managers.
-
class
mode.utils.contexts.
AsyncExitStack
[source]¶ Async context manager for dynamic management of a stack of exit callbacks.
Example
>>> async with AsyncExitStack() as stack: ... connections = [await stack.enter_async_context(get_connection()) ... for i in range(5)] ... # All opened connections will automatically be released at the ... # end of the async with statement, even if attempts to open a ... # connection later in the list raise an exception.
-
async
enter_async_context
(cm: AsyncContextManager) → Any[source]¶ Enters the supplied async context manager.
If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method.
-
push_async_exit
(exit: Union[AsyncContextManager, Callable[..., Awaitable]]) → Union[AsyncContextManager, Callable[..., Awaitable]][source]¶ Register coroutine with the standard __aexit__ method signature.
Can suppress exceptions the same way __aexit__ method can. Also accepts any object with an __aexit__ method (registering a call to the method instead of the object itself).
-
async
-
class
mode.utils.contexts.
ExitStack
[source]¶ Context manager for dynamic management of a stack of exit callbacks.
- For example:
- with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of # the with statement, even if attempts to open files later # in the list raise an exception.