summaryrefslogtreecommitdiffstats
path: root/Doc/library/contextlib.rst
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2017-05-01 01:25:58 (GMT)
committerYury Selivanov <yselivanov@gmail.com>2017-05-01 01:25:58 (GMT)
commit2e624690bd74071358566300b7ef0bc45f444a30 (patch)
treef96176f5997f38c00974854907b586ce887981a3 /Doc/library/contextlib.rst
parent9dc2b3809f38be2e403ee264958106badfda142d (diff)
downloadcpython-2e624690bd74071358566300b7ef0bc45f444a30.zip
cpython-2e624690bd74071358566300b7ef0bc45f444a30.tar.gz
cpython-2e624690bd74071358566300b7ef0bc45f444a30.tar.bz2
bpo-29679: Implement @contextlib.asynccontextmanager (#360)
Diffstat (limited to 'Doc/library/contextlib.rst')
-rw-r--r--Doc/library/contextlib.rst30
1 files changed, 30 insertions, 0 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index dd34c96..1979369 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -80,6 +80,36 @@ Functions and classes provided:
Use of :class:`ContextDecorator`.
+.. decorator:: asynccontextmanager
+
+ Similar to :func:`~contextlib.contextmanager`, but creates an
+ :ref:`asynchronous context manager <async-context-managers>`.
+
+ This function is a :term:`decorator` that can be used to define a factory
+ function for :keyword:`async with` statement asynchronous context managers,
+ without needing to create a class or separate :meth:`__aenter__` and
+ :meth:`__aexit__` methods. It must be applied to an :term:`asynchronous
+ generator` function.
+
+ A simple example::
+
+ from contextlib import asynccontextmanager
+
+ @asynccontextmanager
+ async def get_connection():
+ conn = await acquire_db_connection()
+ try:
+ yield
+ finally:
+ await release_db_connection(conn)
+
+ async def get_all_users():
+ async with get_connection() as conn:
+ return conn.query('SELECT ...')
+
+ .. versionadded:: 3.7
+
+
.. function:: closing(thing)
Return a context manager that closes *thing* upon completion of the block. This