Message413830
Also Future.result() and Future.exception() can raise a CancelledError. So a CancelledError raised in a task may not contain a message passed to Task.cancel().
import asyncio
import random
async def main():
fut = asyncio.Future()
fut.cancel()
async def job():
if random.random() < 0.5:
await asyncio.sleep(2)
fut.result()
await asyncio.sleep(5)
task = asyncio.create_task(job())
await asyncio.sleep(1)
task.cancel("cancel task")
await task
asyncio.run(main())
You need to catch a CancelledError raised in a coroutine and re-raise a new CancelledError with the specified cancel message if the task was cancelled. |
|
Date |
User |
Action |
Args |
2022-02-23 17:27:45 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, asvetlov, chris.jerdonek, yselivanov, graingert, bjs, pagliaricci.m |
2022-02-23 17:27:45 | serhiy.storchaka | set | messageid: <1645637265.64.0.953578636346.issue45390@roundup.psfhosted.org> |
2022-02-23 17:27:45 | serhiy.storchaka | link | issue45390 messages |
2022-02-23 17:27:45 | serhiy.storchaka | create | |
|