twisteroid ambassador <[email protected]> added the comment:
There is a way to distinguish whether a task is being cancelled from the
"inside" or "outside", like this:
async def task1func():
task2 = asyncio.create_task(task2func())
try:
await asyncio.wait((task2,))
except asyncio.CancelledError:
print('task1 is being cancelled from outside')
# Optionally cancel task2 here, since asyncio.wait() shields task2
from
# being cancelled from the outside
raise
assert task2.done()
if task2.cancelled():
print('task2 was cancelled')
# Note that task1 is not cancelled here, so if you want to cancel
# task1 as well, do this:
# raise asyncio.CancelledError
task2_result = task2.result()
# Use your result here
----------
nosy: +twisteroid ambassador
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue35945>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com