Skip to content

Commit a1735c5

Browse files
committed
Add doc
1 parent a5fd929 commit a1735c5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Doc/library/contextlib.rst

+37
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)