异步CancelledError和KeyboardInterrupt

异步CancelledError和KeyboardInterrupt,第1张

异步CancelledError和KeyboardInterrupt

task.cancel()
本身并没有完成任务:它只是说
CancelledError
应该在其中提出的任务并立即返回。您应该调用它并等待任务实际上被取消(它会引发
CancelledError
)。

您也不应压制

CancelledError
内部任务。

在尝试显示不同的任务处理方式的地方,请阅读此答案。例如,要取消某些任务并等待其取消,您可以执行以下 *** 作:

from contextlib import suppresstask = ...  # remember, task doesn't suppress CancelledError itselftask.cancel()  # returns immediately, we should await task raised CancelledError.with suppress(asyncio.CancelledError):    await task  # or loop.run_until_complete(task) if it happens after event loop stopped# Now when we awaited for CancelledError and handled it, # task is finally over and we can close event loop without warning.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5666671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存