Message335829
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 |
|
Date |
User |
Action |
Args |
2019-02-18 13:06:28 | twisteroid ambassador | set | recipients:
+ twisteroid ambassador, asvetlov, yselivanov, jnwatson, epiphyte |
2019-02-18 13:06:28 | twisteroid ambassador | set | messageid: <1550495188.15.0.35802053828.issue35945@roundup.psfhosted.org> |
2019-02-18 13:06:28 | twisteroid ambassador | link | issue35945 messages |
2019-02-18 13:06:28 | twisteroid ambassador | create | |
|