summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorTom Gringauz <tomgrin10@gmail.com>2020-11-09 12:34:07 (GMT)
committerGitHub <noreply@github.com>2020-11-09 12:34:07 (GMT)
commita117167d8dc8fa673a4646f509551c7950f824e5 (patch)
tree14ed56dc05c33931932c8e911a76b374d9fd74b1 /Doc/library
parent97e8b1eaeaf3aa325c84ff2e13417c30414d0269 (diff)
downloadcpython-a117167d8dc8fa673a4646f509551c7950f824e5.zip
cpython-a117167d8dc8fa673a4646f509551c7950f824e5.tar.gz
cpython-a117167d8dc8fa673a4646f509551c7950f824e5.tar.bz2
bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/contextlib.rst18
1 files changed, 18 insertions, 0 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst
index d5a1068..91edbba 100644
--- a/Doc/library/contextlib.rst
+++ b/Doc/library/contextlib.rst
@@ -243,8 +243,26 @@ Functions and classes provided:
with cm as file:
# Perform processing on the file
+ It can also be used as a stand-in for
+ :ref:`asynchronous context managers <async-context-managers>`::
+
+ async def send_http(session=None):
+ if not session:
+ # If no http session, create it with aiohttp
+ cm = aiohttp.ClientSession()
+ else:
+ # Caller is responsible for closing the session
+ cm = nullcontext(session)
+
+ async with cm as session:
+ # Send http requests with session
+
.. versionadded:: 3.7
+ .. versionchanged:: 3.10
+ :term:`asynchronous context manager` support was added.
+
+
.. function:: suppress(*exceptions)