Account for removal of aynchio.Tasks.all_tasks

As we still support 3.5..3.7, we need to catch the case that
asyncio.get_running_loop and asyncio.all_tasks are not yet available.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jan Kiszka 2020-11-13 08:29:32 +01:00
parent 79641ac382
commit 85e3b1e74f

View File

@ -84,8 +84,16 @@ def _atexit_handler():
"""
Waits for completion of the event loop
"""
loop = asyncio.get_event_loop()
pending = asyncio.Task.all_tasks()
try:
loop = asyncio.get_running_loop()
pending = asyncio.all_tasks(loop)
except RuntimeError:
# no running loop anymore, nothing to do
return
except AttributeError:
# for Python < 3.7
loop = asyncio.get_event_loop()
pending = asyncio.Task.all_tasks(loop)
if not loop.is_closed():
loop.run_until_complete(asyncio.gather(*pending))
loop.close()