Skip to content

Commit

Permalink
Attempt to fix jupyer running event loop error
Browse files Browse the repository at this point in the history
Using `run_until_complete` in jupyter notebook will throw
a loop is running error.  So the fix is to check if loop is
running and then do alternative logic.  Current fix doesn't
actually work but times out.

Other changes:
- wrap decorator using functools
- check for running event loop
  • Loading branch information
chanhosuh committed Nov 8, 2022
1 parent 5f474af commit b6924fa
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion curvesim/network/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import concurrent
import functools

from gmpy2 import mpz

Expand Down Expand Up @@ -39,9 +41,24 @@ def sync(func):
Sync version of the async function.
"""

@functools.wraps(func)
def inner(*args, event_loop=None, **kwargs):
loop = event_loop or asyncio.get_event_loop()
res = loop.run_until_complete(func(*args, **kwargs))
coro = func(*args, **kwargs)
if loop.is_running():
try:
future = asyncio.run_coroutine_threadsafe(coro, loop)
res = future.result(timeout=60)
except concurrent.futures.TimeoutError as e:
print("The coroutine took too long, cancelling the task...")
future.cancel()
raise e
except Exception as e:
print("The coroutine raised an exception: {!r}".format(e))
raise e
else:
res = loop.run_until_complete(coro)

return res

return inner

0 comments on commit b6924fa

Please sign in to comment.