Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a1735c5

Browse filesBrowse files
committed
Add doc
1 parent a5fd929 commit a1735c5
Copy full SHA for a1735c5

File tree

1 file changed

+37
-0
lines changed
Filter options

1 file changed

+37
-0
lines changed

‎Doc/library/contextlib.rst

Copy file name to clipboardExpand all lines: Doc/library/contextlib.rst
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,43 @@ Functions and classes provided:
351351
.. versionadded:: 3.2
352352

353353

354+
.. class:: AsyncContextManager
355+
356+
Similar as ContextManger only for async
357+
358+
Example of ``ContextDecorator``::
359+
360+
from asyncio import run
361+
from contextlib import AsyncContextDecorator
362+
363+
class mycontext(AsyncContextDecorator):
364+
async def __aenter__(self):
365+
print('Starting')
366+
return self
367+
368+
async def __aexit__(self, *exc):
369+
print('Finishing')
370+
return False
371+
372+
>>> @mycontext()
373+
... async def function():
374+
... print('The bit in the middle')
375+
...
376+
>>> run(function())
377+
Starting
378+
The bit in the middle
379+
Finishing
380+
381+
>>> async def function():
382+
... async with mycontext():
383+
... print('The bit in the middle')
384+
...
385+
>>> run(function())
386+
Starting
387+
The bit in the middle
388+
Finishing
389+
390+
354391
.. class:: ExitStack()
355392

356393
A context manager that is designed to make it easy to programmatically

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.