Skip to content

Commit

Permalink
only raise if the event loop is running
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan King committed Oct 25, 2023
1 parent e977803 commit 6e43cf8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions hail/python/hailtop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,27 @@ 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 '
'yourself with asyncio.set_event_loop before initializing Hail if you want to run '
'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
Expand Down

0 comments on commit 6e43cf8

Please sign in to comment.