Skip to content

Commit

Permalink
push me in the AM
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan King committed Oct 25, 2023
1 parent f53405d commit cda56a8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
20 changes: 3 additions & 17 deletions hail/python/hailtop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,20 @@ def is_notebook() -> bool:
return IS_NOTEBOOK


_hail_created_the_main_thread_event_loop = False


def hail_event_loop():
'''If an event loop exists and Hail did not create it, use nest_asyncio to allow Hail's event
loops to nest inside it.
If an event loop exists and Hail did create it, then a developer is trying to use async inside
sync inside async. That is forbidden.
'''If a running event loop exists, use nest_asyncio to allow Hail's event loops to nest inside
it.
If no event loop exists, ask asyncio to get one for us.
'''
global _hail_created_the_main_thread_event_loop

import asyncio # pylint: disable=import-outside-toplevel
import nest_asyncio # pylint: disable=import-outside-toplevel

try:
asyncio.get_running_loop()

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.'
)

nest_asyncio.apply()
return asyncio.get_running_loop()
except RuntimeError as err:
except RuntimeError:
return asyncio.get_event_loop()
2 changes: 1 addition & 1 deletion hail/python/hailtop/fs/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def _fs() -> RouterFS:
'''Return an FS for this particular thread.'''
global _router_fs
global _router_fs_for_loop

loop = hail_event_loop()
if fs := _router_fs_for_loop.get(loop):
Expand Down
2 changes: 1 addition & 1 deletion hail/python/hailtop/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self,
**kwargs):
try:
self.loop_at_creation_time = asyncio.get_running_loop()
except RuntimeError as err:
except RuntimeError:
raise ValueError(
'aiohttp.ClientSession stashes a copy of the event loop into a field. Very confusing '
'errors arise if that stashed event loop differs from the currently running one. '
Expand Down

0 comments on commit cda56a8

Please sign in to comment.