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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…