Skip to content

Commit

Permalink
always call nest asyncio in magics
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfast committed Feb 2, 2024
1 parent a42a7df commit 66ce052
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/midgy/_ipython.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from io import StringIO
from .weave import quick_doctest, weave_argv

NEST_ASYNC = None


def post_run_cell(result):
from IPython.display import display, Markdown
Expand Down Expand Up @@ -37,6 +39,7 @@ def unload_ipython_extension(shell):


def run_ipython(source, _retry=False):
global NEST_ASYNC
from asyncio import get_event_loop
from IPython import get_ipython
from IPython.core.interactiveshell import ExecutionResult, ExecutionInfo
Expand All @@ -52,27 +55,20 @@ def run_ipython(source, _retry=False):
module = compile(source, filename, "exec", PyCF_ONLY_AST)
module = shell.transform_ast(module)
result = ExecutionResult(ExecutionInfo(source, False, False, False, filename))
try:
get_event_loop().run_until_complete(
shell.run_ast_nodes(
module.body,
filename,
compiler=shell.compile,
result=result,
interactivity=interactivity,
)
)
except RuntimeError:

if NEST_ASYNC is None:
import nest_asyncio

nest_asyncio.apply()
get_event_loop().run_until_complete(
shell.run_ast_nodes(
module.body,
filename,
compiler=shell.compile,
result=result,
interactivity=interactivity,
)
NEST_ASYNC = True

get_event_loop().run_until_complete(
shell.run_ast_nodes(
module.body,
filename,
compiler=shell.compile,
result=result,
interactivity=interactivity,
)
)
return

0 comments on commit 66ce052

Please sign in to comment.