From 6e43cf85122f90a75629c080aa436664040dbc4f Mon Sep 17 00:00:00 2001 From: Dan King Date: Tue, 24 Oct 2023 20:24:07 -0400 Subject: [PATCH] only raise if the event loop is running --- hail/python/hailtop/__init__.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hail/python/hailtop/__init__.py b/hail/python/hailtop/__init__.py index 67d8a9d87255..ed95a4f00f5e 100644 --- a/hail/python/hailtop/__init__.py +++ b/hail/python/hailtop/__init__.py @@ -46,18 +46,13 @@ def main_thread_event_loop(): import threading # pylint: disable=import-outside-toplevel import nest_asyncio # pylint: disable=import-outside-toplevel - if _hail_created_the_main_thread_event_loop: - raise ValueError( - 'As a matter of Hail team policy, you are not allowed to nest asynchronous Hail code ' - 'inside synchronous Hail code.' - ) - try: asyncio.get_running_loop() nest_asyncio.apply() return asyncio.get_running_loop() except RuntimeError as err: assert err.args[0] == "no running event loop" + if threading.current_thread() is not threading.main_thread(): raise ValueError( 'Hail will not create an event loop outside of the Main Thread. Create one ' @@ -65,6 +60,13 @@ def main_thread_event_loop(): 'Hail outside the Main Thread. Note that Hail is not thread-safe. Current thread: ' f'{threading.current_thread()}' ) from err + + if _hail_created_the_main_thread_event_loop: + raise ValueError( + 'As a matter of Hail team policy, you are not allowed to nest asynchronous Hail code ' + 'inside synchronous Hail code.' + ) from err + loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) _hail_created_the_main_thread_event_loop = True