diff options
author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2017-05-01 01:25:58 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@gmail.com> | 2017-05-01 01:25:58 (GMT) |
commit | 2e624690bd74071358566300b7ef0bc45f444a30 (patch) | |
tree | f96176f5997f38c00974854907b586ce887981a3 /Doc | |
parent | 9dc2b3809f38be2e403ee264958106badfda142d (diff) | |
download | cpython-2e624690bd74071358566300b7ef0bc45f444a30.zip cpython-2e624690bd74071358566300b7ef0bc45f444a30.tar.gz cpython-2e624690bd74071358566300b7ef0bc45f444a30.tar.bz2 |
bpo-29679: Implement @contextlib.asynccontextmanager (#360)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/contextlib.rst | 30 | ||||
-rw-r--r-- | Doc/reference/datamodel.rst | 2 | ||||
-rw-r--r-- | Doc/whatsnew/3.7.rst | 6 |
3 files changed, 38 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 diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 4b49bfd..25afc35 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2575,6 +2575,8 @@ An example of an asynchronous iterable object:: result in a :exc:`RuntimeError`. +.. _async-context-managers: + Asynchronous Context Managers ----------------------------- diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 875fc55..cb0086c 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -95,6 +95,12 @@ New Modules Improved Modules ================ +contextlib +---------- + +:func:`contextlib.asynccontextmanager` has been added. (Contributed by +Jelle Zijlstra in :issue:`29679`.) + distutils --------- |