7.11. AsyncIO ShieldΒΆ

  • Shielding from Cancellation

  • Awaitable asyncio.shield(aw)

  • Protect an awaitable object from being cancelled.

7.11.1. ExampleΒΆ

>>> import asyncio
>>>
>>>
>>> async def work():
...     return 'done'
>>>
>>>
>>> async def main():
...     try:
...         res = await asyncio.shield(work())
...     except asyncio.CancelledError:
...         res = None
>>>
>>>
>>> asyncio.run(main())