Skip to content

Commit

Permalink
call wait_for_ready() in runapp
Browse files Browse the repository at this point in the history
ensures kernel is ready before running

avoids lost output if iopub isn't connected yet

and shutdown kernel when finished, rather than relying on the kernel shutting itself down
  • Loading branch information
minrk committed Sep 23, 2024
1 parent c37bfc5 commit 3f8dd20
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions jupyter_client/runapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
# Distributed under the terms of the Modified BSD License.
from __future__ import annotations

import queue
import atexit
import signal
import sys
import time
import typing as t

from jupyter_core.application import JupyterApp, base_aliases, base_flags
Expand Down Expand Up @@ -73,7 +72,8 @@ def initialize(self, argv: list[str] | None = None) -> None: # type:ignore[over
super().initialize(argv)
JupyterConsoleApp.initialize(self)
signal.signal(signal.SIGINT, self.handle_sigint)
self.init_kernel_info()
if self.kernel_manager:
atexit.register(self.kernel_manager.shutdown_kernel)

def handle_sigint(self, *args: t.Any) -> None:
"""Handle SIGINT."""
Expand All @@ -82,28 +82,11 @@ def handle_sigint(self, *args: t.Any) -> None:
else:
self.log.error("Cannot interrupt kernels we didn't start.\n")

def init_kernel_info(self) -> None:
"""Wait for a kernel to be ready, and store kernel info"""
timeout = self.kernel_timeout
tic = time.time()
self.kernel_client.hb_channel.unpause()
msg_id = self.kernel_client.kernel_info()
while True:
try:
reply = self.kernel_client.get_shell_msg(timeout=1)
except queue.Empty as e:
if (time.time() - tic) > timeout:
msg = "Kernel didn't respond to kernel_info_request"
raise RuntimeError(msg) from e
else:
if reply["parent_header"].get("msg_id") == msg_id:
self.kernel_info = reply["content"]
return

def start(self) -> None:
"""Start the application."""
self.log.debug("jupyter run: starting...")
super().start()
self.kernel_client.wait_for_ready(timeout=self.kernel_timeout)
if self.filenames_to_run:
for filename in self.filenames_to_run:
self.log.debug("jupyter run: executing `%s`", filename)
Expand All @@ -112,8 +95,10 @@ def start(self) -> None:
reply = self.kernel_client.execute_interactive(code, timeout=OUTPUT_TIMEOUT)
return_code = 0 if reply["content"]["status"] == "ok" else 1
if return_code:
raise Exception("jupyter-run error running '%s'" % filename)
msg = f"jupyter-run error running '{filename}'"
raise Exception(msg)
else:
self.log.debug("jupyter run: executing from stdin")
code = sys.stdin.read()
reply = self.kernel_client.execute_interactive(code, timeout=OUTPUT_TIMEOUT)
return_code = 0 if reply["content"]["status"] == "ok" else 1
Expand Down

0 comments on commit 3f8dd20

Please sign in to comment.