diff options
author | Ilya Kulakov <kulakov.ilya@gmail.com> | 2018-01-25 20:51:18 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2018-01-25 20:51:18 (GMT) |
commit | 1aa094f74039cd20fdc7df56c68f6848c18ce4dd (patch) | |
tree | 7390595ae9e64c0dc0e7b64254848d8acb2c8928 /Doc/library/contextlib.rst | |
parent | 6ab62920c87930dedc31fe633ecda3e51d3d7503 (diff) | |
download | cpython-1aa094f74039cd20fdc7df56c68f6848c18ce4dd.zip cpython-1aa094f74039cd20fdc7df56c68f6848c18ce4dd.tar.gz cpython-1aa094f74039cd20fdc7df56c68f6848c18ce4dd.tar.bz2 |
bpo-29302: Implement contextlib.AsyncExitStack. (#4790)
Diffstat (limited to 'Doc/library/contextlib.rst')
-rw-r--r-- | Doc/library/contextlib.rst | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index faa6c8a..54d3a8e 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -435,6 +435,44 @@ Functions and classes provided: callbacks registered, the arguments passed in will indicate that no exception occurred. +.. class:: AsyncExitStack() + + An :ref:`asynchronous context manager <async-context-managers>`, similar + to :class:`ExitStack`, that supports combining both synchronous and + asynchronous context managers, as well as having coroutines for + cleanup logic. + + The :meth:`close` method is not implemented, :meth:`aclose` must be used + instead. + + .. method:: enter_async_context(cm) + + Similar to :meth:`enter_context` but expects an asynchronous context + manager. + + .. method:: push_async_exit(exit) + + Similar to :meth:`push` but expects either an asynchronous context manager + or a coroutine. + + .. method:: push_async_callback(callback, *args, **kwds) + + Similar to :meth:`callback` but expects a coroutine. + + .. method:: aclose() + + Similar to :meth:`close` but properly handles awaitables. + + Continuing the example for :func:`asynccontextmanager`:: + + 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. + + .. versionadded:: 3.7 Examples and Recipes -------------------- |