【发布时间】:2023-07-10 01:31:01
【问题描述】:
例子:
with suppress(asyncio.CancelledError):
[await t for t in asyncio.all_tasks(loop=self.loop)
if t is not asyncio.current_task()]
为避免Task was destroyed but it is pending! 警告,我必须在取消后等待任务,但等待它们会导致终端被CancelledError 发送垃圾邮件。我知道它已取消,但我不需要看到它。
在这里使用contextlib.suppress 是否会对取消产生负面影响?我可以避免看到已取消错误(或任务已销毁警告而无需等待)的唯一其他方法是使用asyncio.wait 而不是asyncio.gather 开始我的初始任务。出于某种原因,wait 似乎抑制了异常。我在wait 上使用return_when=asyncio.FIRST_EXCEPTION,在gather 上使用return_exceptions=True。但似乎无论我如何设置他们的关键字参数,gather 都会打印异常,而 wait 不会。
【问题讨论】:
标签: python task python-asyncio cancellation graceful-shutdown