Skip to content

Commit

Permalink
add async support for kernel shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed Feb 25, 2019
1 parent 5103959 commit 3cabdc4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions jupyter_client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def request_shutdown(self, restart=False):
self._connect_control_socket()
self.session.send(self._control_socket, msg)

@gen.coroutine
def finish_shutdown(self, waittime=None, pollinterval=0.1):
"""Wait for kernel shutdown, then kill process if it doesn't shutdown.
Expand All @@ -270,7 +271,7 @@ def finish_shutdown(self, waittime=None, pollinterval=0.1):
waittime = max(self.shutdown_wait_time, 0)
for i in range(int(waittime/pollinterval)):
if self.is_alive():
time.sleep(pollinterval)
yield gen.sleep(pollinterval)
else:
# If there's still a proc, wait and clear
if self.has_kernel:
Expand All @@ -292,6 +293,7 @@ def cleanup(self, connection_file=True):
self._close_control_socket()
self.session.parent = None

@gen.coroutine
def shutdown_kernel(self, now=False, restart=False):
"""Attempts to stop the kernel process cleanly.
Expand Down Expand Up @@ -320,7 +322,7 @@ def shutdown_kernel(self, now=False, restart=False):
# Don't send any additional kernel kill messages immediately, to give
# the kernel a chance to properly execute shutdown actions. Wait for at
# most 1s, checking every 0.1s.
self.finish_shutdown()
yield gen.maybe_future(self.finish_shutdown())

self.cleanup(connection_file=not restart)

Expand Down
8 changes: 6 additions & 2 deletions jupyter_client/multikernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def start_kernel(self, kernel_name=None, **kwargs):
self._kernels[kernel_id] = km
raise gen.Return(kernel_id)

@kernel_method
@gen.coroutine
def shutdown_kernel(self, kernel_id, now=False, restart=False):
"""Shutdown a kernel by its kernel uuid.
Expand All @@ -125,17 +125,21 @@ def shutdown_kernel(self, kernel_id, now=False, restart=False):
restart : bool
Will the kernel be restarted?
"""
km = self.get_kernel(kernel_id)
yield gen.maybe_future(km.shutdown_kernel(now, restart))
self.log.info("Kernel shutdown: %s" % kernel_id)
self.remove_kernel(kernel_id)

@kernel_method
def request_shutdown(self, kernel_id, restart=False):
"""Ask a kernel to shut down by its kernel uuid"""

@kernel_method
@gen.coroutine
def finish_shutdown(self, kernel_id, waittime=None, pollinterval=0.1):
"""Wait for a kernel to finish shutting down, and kill it if it doesn't
"""
km = self.get_kernel(kernel_id)
yield gen.maybe_future(km.finish_shutdown(waittime, pollinterval))
self.log.info("Kernel shutdown: %s" % kernel_id)

@kernel_method
Expand Down

0 comments on commit 3cabdc4

Please sign in to comment.