From 20f5c1b0cf6941eb0a3ee341cedeaa67e4290073 Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 18 Jun 2012 17:50:30 -0700 Subject: [PATCH 1/2] enable graceful restart of kernels in notebook --- IPython/frontend/html/notebook/kernelmanager.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/IPython/frontend/html/notebook/kernelmanager.py b/IPython/frontend/html/notebook/kernelmanager.py index 3ea27a9a3..2833d6303 100644 --- a/IPython/frontend/html/notebook/kernelmanager.py +++ b/IPython/frontend/html/notebook/kernelmanager.py @@ -43,7 +43,7 @@ class MultiKernelManager(LoggingConfigurable): """A class for managing multiple kernels.""" kernel_manager_class = DottedObjectName( - "IPython.zmq.kernelmanager.KernelManager", config=True, + "IPython.zmq.blockingkernelmanager.BlockingKernelManager", config=True, help="""The kernel manager class. This is configurable to allow subclassing of the KernelManager for customized behavior. """ @@ -87,6 +87,8 @@ class MultiKernelManager(LoggingConfigurable): config=self.config, ) km.start_kernel(**kwargs) + # start the shell channel, needed for graceful restart + km.start_channels(shell=True, sub=False, stdin=False, hb=False) self._kernels[kernel_id] = km return kernel_id @@ -284,7 +286,7 @@ class MappingKernelManager(MultiKernelManager): """Restart a kernel while keeping clients connected.""" self._check_kernel_id(kernel_id) km = self.get_kernel(kernel_id) - km.restart_kernel(now=True) + km.restart_kernel() self.log.info("Kernel restarted: %s" % kernel_id) return kernel_id From 61bbd632ef70a48c728bc8ab86fdc5488e4fbd61 Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 18 Jun 2012 17:52:04 -0700 Subject: [PATCH 2/2] use shutdown_kernel instead of hard kill in notebook --- IPython/frontend/html/notebook/handlers.py | 2 +- .../frontend/html/notebook/kernelmanager.py | 20 ++++++++++++++++++- IPython/frontend/html/notebook/notebookapp.py | 4 ++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index bc5dc9e1f..f2030e702 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -344,7 +344,7 @@ class KernelHandler(AuthenticatedHandler): @web.authenticated def delete(self, kernel_id): km = self.application.kernel_manager - km.kill_kernel(kernel_id) + km.shutdown_kernel(kernel_id) self.set_status(204) self.finish() diff --git a/IPython/frontend/html/notebook/kernelmanager.py b/IPython/frontend/html/notebook/kernelmanager.py index 2833d6303..a31a58afe 100644 --- a/IPython/frontend/html/notebook/kernelmanager.py +++ b/IPython/frontend/html/notebook/kernelmanager.py @@ -87,11 +87,22 @@ class MultiKernelManager(LoggingConfigurable): config=self.config, ) km.start_kernel(**kwargs) - # start the shell channel, needed for graceful restart + # start just the shell channel, needed for graceful restart km.start_channels(shell=True, sub=False, stdin=False, hb=False) self._kernels[kernel_id] = km return kernel_id + def shutdown_kernel(self, kernel_id): + """Shutdown a kernel by its kernel uuid. + + Parameters + ========== + kernel_id : uuid + The id of the kernel to shutdown. + """ + self.get_kernel(kernel_id).shutdown_kernel() + del self._kernels[kernel_id] + def kill_kernel(self, kernel_id): """Kill a kernel by its kernel uuid. @@ -269,6 +280,13 @@ class MappingKernelManager(MultiKernelManager): self.log.info("Using existing kernel: %s" % kernel_id) return kernel_id + def shutdown_kernel(self, kernel_id): + """Shutdown a kernel and remove its notebook association.""" + self._check_kernel_id(kernel_id) + super(MappingKernelManager, self).shutdown_kernel(kernel_id) + self.delete_mapping_for_kernel(kernel_id) + self.log.info("Kernel shutdown: %s" % kernel_id) + def kill_kernel(self, kernel_id): """Kill a kernel and remove its notebook association.""" self._check_kernel_id(kernel_id) diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index 3b49b3945..6059fb3b8 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -532,9 +532,9 @@ class NotebookApp(BaseIPythonApplication): """ self.log.info('Shutting down kernels') km = self.kernel_manager - # copy list, since kill_kernel deletes keys + # copy list, since shutdown_kernel deletes keys for kid in list(km.kernel_ids): - km.kill_kernel(kid) + km.shutdown_kernel(kid) def start(self): ip = self.ip if self.ip else '[all ip addresses on your system]'