From 85e3b1e74f99e5d9f671431a852d3d4cfc46a7c0 Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Fri, 13 Nov 2020 08:29:32 +0100 Subject: [PATCH] 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 --- kas/kas.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/kas/kas.py b/kas/kas.py index a71b3e8..272c509 100644 --- a/kas/kas.py +++ b/kas/kas.py @@ -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()