From 8a12ea9c110c6ce0e5ea12b2a6a82c7ab68ade06 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 13 Sep 2021 14:58:41 +0100 Subject: [PATCH 1/2] Close new event loops in just_run helper --- nbclient/util.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/nbclient/util.py b/nbclient/util.py index 9b672357..bafcb778 100644 --- a/nbclient/util.py +++ b/nbclient/util.py @@ -37,24 +37,29 @@ def just_run(coro: Awaitable) -> Any: """Make the coroutine run, even if there is an event loop running (using nest_asyncio)""" # original from vaex/asyncio.py loop = asyncio._get_running_loop() - if loop is None: - had_running_loop = False - try: - loop = asyncio.get_event_loop() - except RuntimeError: - # we can still get 'There is no current event loop in ...' - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - else: - had_running_loop = True - if had_running_loop: + if loop is not None: # if there is a running loop, we patch using nest_asyncio # to have reentrant event loops check_ipython() import nest_asyncio nest_asyncio.apply() check_patch_tornado() - return loop.run_until_complete(coro) + return loop.run_until_complete(coro) + + elif hasattr(asyncio, 'run'): # Python 3.7 + + return asyncio.run(coro) + + else: # Python 3.6 + try: + loop = asyncio.get_event_loop() + except RuntimeError: + # we can still get 'There is no current event loop in ...' + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + return loop.run_until_complete(coro) + finally: + loop.close() def run_sync(coro: Callable) -> Callable: From 8b6ea2dab70fe9253757573ee52ad0bf5680c5cb Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 13 Sep 2021 17:07:15 +0100 Subject: [PATCH 2/2] Use temporary event loop when one is not already running in just_run --- nbclient/util.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/nbclient/util.py b/nbclient/util.py index bafcb778..7b987e94 100644 --- a/nbclient/util.py +++ b/nbclient/util.py @@ -37,7 +37,15 @@ def just_run(coro: Awaitable) -> Any: """Make the coroutine run, even if there is an event loop running (using nest_asyncio)""" # original from vaex/asyncio.py loop = asyncio._get_running_loop() - if loop is not None: + if loop is None: + # Create a temporary event loop + loop = asyncio.new_event_loop() + try: + return loop.run_until_complete(coro) + finally: + loop.close() + + else: # if there is a running loop, we patch using nest_asyncio # to have reentrant event loops check_ipython() @@ -46,21 +54,6 @@ def just_run(coro: Awaitable) -> Any: check_patch_tornado() return loop.run_until_complete(coro) - elif hasattr(asyncio, 'run'): # Python 3.7 + - return asyncio.run(coro) - - else: # Python 3.6 - try: - loop = asyncio.get_event_loop() - except RuntimeError: - # we can still get 'There is no current event loop in ...' - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - try: - return loop.run_until_complete(coro) - finally: - loop.close() - def run_sync(coro: Callable) -> Callable: """Runs a coroutine and blocks until it has executed.