Example:
with suppress(asyncio.CancelledError):
[await t for t in asyncio.all_tasks(loop=self.loop)
if t is not asyncio.current_task()]
To avoid Task was destroyed but it is pending!
warning, I have to await the tasks after cancelling, but awaiting them leads to the terminal being spammed with CancelledError
. I know it's cancelled but I don't need to see that.
Does using contextlib.suppress
here intervene negatively with the cancellation? The only other way that I can avoid seeing the cancelled error (or task destroyed warning without awaiting) is to start my initial tasks with asyncio.wait
rather than asyncio.gather
. For some reason, wait
appears to suppress exceptions. I use return_when=asyncio.FIRST_EXCEPTION
on wait
and return_exceptions=True
on gather
. But it seems that regardless of how I set their keyword args, gather
prints exceptions while wait
does not.