How to cancel Task execution even if it ignored CancelledError?

后端 未结 1 1309
陌清茗
陌清茗 2020-12-21 16:46

This is example of cancelling task:

import asyncio


async def some_func():
    await asyncio.sleep(2)
    print(\'Haha! Task keeps running!\')
    await asy         


        
1条回答
  •  轮回少年
    2020-12-21 17:15

    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.

    0 讨论(0)
提交回复
热议问题