Merge pull request #1981 from tkf/kill-bg-processes

Clean BG processes created by %%script on kernel exit

* uses less forceful shutdown of kernels in the notebook, allowing atexit machinery to fire
* enables daemon BackgroundJobs
* cleanup %%script --bg subprocesses at shutdown
This commit is contained in:
Min RK 2012-06-20 13:35:56 -07:00
commit e306daad47
3 changed files with 25 additions and 5 deletions

View File

@ -345,7 +345,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()

View File

@ -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,9 +87,22 @@ class MultiKernelManager(LoggingConfigurable):
config=self.config,
)
km.start_kernel(**kwargs)
# 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.
@ -266,6 +279,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)
@ -283,7 +303,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

View File

@ -537,9 +537,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]'