Skip to content

Commit

Permalink
Merge pull request ipython#2369 from epatters/block-after-kill
Browse files Browse the repository at this point in the history
Block until kernel termination after sending a kill signal.  This will make for more robust kernel restarting in the Qt console.
  • Loading branch information
fperez committed Sep 27, 2012
2 parents 51b939d + 7966714 commit 0f65d3d
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions IPython/zmq/kernelmanager.py
Expand Up @@ -812,8 +812,9 @@ def start_kernel(self, **kw):
self.kernel = launch_kernel(fname=self.connection_file, **kw)

def shutdown_kernel(self, restart=False):
""" Attempts to the stop the kernel process cleanly. If the kernel
cannot be stopped, it is killed, if possible.
""" Attempts to the stop the kernel process cleanly.
If the kernel cannot be stopped and the kernel is local, it is killed.
"""
# FIXME: Shutdown does not work on Windows due to ZMQ errors!
if sys.platform == 'win32':
Expand Down Expand Up @@ -894,13 +895,17 @@ def has_kernel(self):
return self.kernel is not None

def kill_kernel(self):
""" Kill the running kernel. """
""" Kill the running kernel.
This method blocks until the kernel process has terminated.
"""
if self.has_kernel:
# Pause the heart beat channel if it exists.
if self._hb_channel is not None:
self._hb_channel.pause()

# Attempt to kill the kernel.
# Signal the kernel to terminate (sends SIGKILL on Unix and calls
# TerminateProcess() on Win32).
try:
self.kernel.kill()
except OSError as e:
Expand All @@ -915,13 +920,18 @@ def kill_kernel(self):
from errno import ESRCH
if e.errno != ESRCH:
raise

# Block until the kernel terminates.
self.kernel.wait()
self.kernel = None
else:
raise RuntimeError("Cannot kill kernel. No kernel is running!")

def interrupt_kernel(self):
""" Interrupts the kernel. Unlike ``signal_kernel``, this operation is
well supported on all platforms.
""" Interrupts the kernel.
Unlike ``signal_kernel``, this operation is well supported on all
platforms.
"""
if self.has_kernel:
if sys.platform == 'win32':
Expand All @@ -933,8 +943,10 @@ def interrupt_kernel(self):
raise RuntimeError("Cannot interrupt kernel. No kernel is running!")

def signal_kernel(self, signum):
""" Sends a signal to the kernel. Note that since only SIGTERM is
supported on Windows, this function is only useful on Unix systems.
""" Sends a signal to the kernel.
Note that since only SIGTERM is supported on Windows, this function is
only useful on Unix systems.
"""
if self.has_kernel:
self.kernel.send_signal(signum)
Expand Down

0 comments on commit 0f65d3d

Please sign in to comment.