File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -351,6 +351,43 @@ Functions and classes provided:
351
351
.. versionadded :: 3.2
352
352
353
353
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
+
354
391
.. class :: ExitStack()
355
392
356
393
A context manager that is designed to make it easy to programmatically
You can’t perform that action at this time.
0 commit comments