Skip to content

Commit

Permalink
Revert "Enable async JupyterApp" (#385)
Browse files Browse the repository at this point in the history
This reverts commit e33fb74.
  • Loading branch information
blink1073 authored Dec 31, 2023
1 parent 59cfd29 commit 42ceac8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 62 deletions.
32 changes: 4 additions & 28 deletions jupyter_core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
jupyter_path,
jupyter_runtime_dir,
)
from .utils import ensure_dir_exists, get_event_loop
from .utils import ensure_dir_exists

# mypy: disable-error-code="no-untyped-call"

Expand Down Expand Up @@ -259,11 +259,6 @@ def initialize(self, argv: t.Any = None) -> None:
self.update_config(cl_config)
if allow_insecure_writes:
issue_insecure_write_warning()
return

async def initialize_async(self) -> None:
"""Perform async initialization of the application, will be called
after synchronous initialize."""

def start(self) -> None:
"""Start the whole thing"""
Expand All @@ -279,33 +274,14 @@ def start(self) -> None:
self.write_default_config()
raise NoStart()

return

async def start_async(self) -> None:
"""Perform async start of the app, will be called after sync start."""

@classmethod
async def _async_launch_instance(cls, argv: t.Any = None, **kwargs: t.Any) -> None:
"""Launch the instance from inside an event loop."""
def launch_instance(cls, argv: t.Any = None, **kwargs: t.Any) -> None:
"""Launch an instance of a Jupyter Application"""
try:
app = cls.instance(**kwargs)
app.initialize(argv)
await app.initialize_async()
app.start()
await app.start_async()
super().launch_instance(argv=argv, **kwargs)
except NoStart:
return

@classmethod
def launch_instance(cls, argv: t.Any = None, **kwargs: t.Any) -> None:
"""Launch a global instance of this Application
If a global instance already exists, this reinitializes and starts it
"""
loop = get_event_loop()
coro = cls._async_launch_instance(argv, **kwargs)
loop.run_until_complete(coro)


if __name__ == "__main__":
JupyterApp.launch_instance()
32 changes: 12 additions & 20 deletions jupyter_core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,18 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
except RuntimeError:
pass

loop = get_event_loop()
return loop.run_until_complete(inner)
# Run the loop for this thread.
# In Python 3.12, a deprecation warning is raised, which
# may later turn into a RuntimeError. We handle both
# cases.
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(inner)

wrapped.__doc__ = coro.__doc__
return wrapped
Expand All @@ -184,21 +194,3 @@ async def ensure_async(obj: Awaitable[T] | T) -> T:
return result
# obj doesn't need to be awaited
return cast(T, obj)


def get_event_loop() -> asyncio.AbstractEventLoop:
# Get the loop for this thread.
# In Python 3.12, a deprecation warning is raised, which
# may later turn into a RuntimeError. We handle both
# cases.
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
if sys.platform == "win32":
loop = asyncio.WindowsSelectorEventLoopPolicy().new_event_loop()
else:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop
14 changes: 0 additions & 14 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,3 @@ def test_runtime_dir_changed():
app.runtime_dir = td
assert os.path.isdir(td)
shutil.rmtree(td)


class AsyncApp(JupyterApp):
async def initialize_async(self):
self.value = 10

async def start_async(self):
assert self.value == 10


def test_async_app():
AsyncApp.launch_instance([])
app = AsyncApp.instance()
assert app.value == 10

0 comments on commit 42ceac8

Please sign in to comment.