bpo-31033: Make traceback for cancelled asyncio tasks more useful by cjerdonek · Pull Request #19951 · python/cpython (original) (raw)
Here's an example to show you what the output look like. For this code:
import asyncio, sys
async def nested(): fut = asyncio.sleep(1) await fut # AWAIT #1
async def run(): task = asyncio.create_task(nested()) await asyncio.sleep(0) task.cancel('POSSIBLY LONG CANCEL MESSAGE')
await task # AWAIT #2
loop = asyncio.new_event_loop() try: loop.run_until_complete(run()) finally: loop.close()
Here is before the PR:
Traceback (most recent call last):
File "/.../cpython/test-cancel.py", line 17, in <module>
loop.run_until_complete(run())
File "/.../cpython/Lib/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
asyncio.exceptions.CancelledError: POSSIBLY LONG CANCEL MESSAGE
And here is after the PR:
Traceback (most recent call last):
File "/.../cpython/test-cancel.py", line 6, in nested
await fut # AWAIT #1
File "/.../cpython/Lib/asyncio/tasks.py", line 669, in sleep
return await future
asyncio.exceptions.CancelledError: POSSIBLY LONG CANCEL MESSAGE
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/.../cpython/test-cancel.py", line 13, in run
await task # AWAIT #2
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/.../cpython/test-cancel.py", line 17, in <module>
loop.run_until_complete(run())
File "/.../cpython/Lib/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
asyncio.exceptions.CancelledError