This is example of cancelling task:
import asyncio
async def some_func():
await asyncio.sleep(2)
print(\'Haha! Task keeps running!\')
await asy
You cannot cancel task that suppresses CancelledError
.
This is similar to impossibility to close generator which ignores GeneratorExit
.
This is intentional behavior. Task may want to do some extra work (e.g. resource cleanup) on cancelling, thus catching CancelledError
may be good idea but suppressing usually is sign of programming error.
Python usually allows you to shoot own feet if you have uncompromising intention to do this.
Catching all exceptions even forbids closing python process by pressing because it's translated into KeyboardInterrupt
internally.