Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 views
in Technique[技术] by (71.8m points)

python - How to cancel Task execution even if it ignored CancelledError?

This is example of cancelling task:

import asyncio


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

async def cancel(task):
    await asyncio.sleep(1)
    task.cancel()

async def main():
    func_task = asyncio.ensure_future(some_func())
    cancel_task = asyncio.ensure_future(cancel(func_task))
    try:
        await func_task
    except asyncio.CancelledError:
        print('Task cancelled as expected')

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

# Task cancelled as expected
# [Finished in 1.2s]

It works ok, task was cancelled. If CancelledError caught inside some_func task wouldn't be cancelled:

async def some_func():
    try:
        await asyncio.sleep(2)
    except:
        pass
    print('Haha! Task keeps running!')
    await asyncio.sleep(2)

# Haha! Task keeps running!
# [Finished in 3.2s]

It can be easy to forgot I shouldn't suppress exceptions anywhere inside async code (or some_func can be third party code, for example), but task should be cancelled. Is there anyway I can do that? Or ignored CancelledError means task can't be cancelled at all?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <Ctrl+C> because it's translated into KeyboardInterrupt internally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...