You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
525 B
Python
22 lines
525 B
Python
import asyncio
|
|
|
|
|
|
async def shield_and_wait(task):
|
|
"""
|
|
Works like asyncio.shield(), but waits for the task to finish before raising CancelledError to the caller.
|
|
"""
|
|
|
|
if not isinstance(task, asyncio.Task):
|
|
task = asyncio.create_task(task)
|
|
|
|
cancel_exc = None
|
|
while True:
|
|
try:
|
|
result = await asyncio.shield(task)
|
|
break
|
|
except asyncio.CancelledError as e:
|
|
cancel_exc = e
|
|
if cancel_exc is not None:
|
|
raise cancel_exc
|
|
return result
|