##// END OF EJS Templates
move multikernelmanager to IPython.kernel
move multikernelmanager to IPython.kernel

File last commit:

r9371:8c06dce5
r9371:8c06dce5
Show More
kernelmanager.py
133 lines | 5.2 KiB | text/x-python | PythonLexer
MinRK
move multikernelmanager to IPython.kernel
r9371 """A kernel manager relating notebooks and kernels
Brian E. Granger
More review changes....
r4609
Authors:
* Brian Granger
"""
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343
Brian E. Granger
Refactoring the notebook app to support the new config system.
r4344 #-----------------------------------------------------------------------------
MinRK
move multikernelmanager to IPython.kernel
r9371 # Copyright (C) 2013 The IPython Development Team
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 #
# Distributed under the terms of the BSD License. The full license is in
Brian E. Granger
More review changes....
r4609 # the file COPYING, distributed as part of this software.
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 #-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
Brian E. Granger
Refactoring the notebook app to support the new config system.
r4344 # Imports
#-----------------------------------------------------------------------------
Brian E. Granger
Adding kernel/notebook associations.
r4494 from tornado import web
MinRK
move multikernelmanager to IPython.kernel
r9371 from IPython.kernel.multikernelmanager import MultiKernelManager
MinRK
make MultiKernelManager.kernel_manager_class configurable...
r6317 from IPython.utils.traitlets import (
MinRK
move multikernelmanager to IPython.kernel
r9371 Dict, List, Unicode, Float, Integer,
MinRK
make MultiKernelManager.kernel_manager_class configurable...
r6317 )
Brian E. Granger
Refactoring the notebook app to support the new config system.
r4344 #-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Brian Granger
Work on the server side of the html notebook.
r4297
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545
MinRK
use zmq.KernelManager to manage individual kernels in notebook...
r4960 class MappingKernelManager(MultiKernelManager):
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 """A KernelManager that handles notebok mapping and HTTP error handling"""
Brian E. Granger
Adding kernel/notebook associations.
r4494
kernel_argv = List(Unicode)
MinRK
add first_beat delay to notebook heartbeats...
r5812
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 time_to_dead = Float(3.0, config=True, help="""Kernel heartbeat interval in seconds.""")
MinRK
add first_beat delay to notebook heartbeats...
r5812 first_beat = Float(5.0, config=True, help="Delay (in seconds) before sending first heartbeat.")
MinRK
add Integer traitlet...
r5344 max_msg_size = Integer(65536, config=True, help="""
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 The max raw message size accepted from the browser
over a WebSocket connection.
""")
Brian E. Granger
Adding kernel/notebook associations.
r4494
_notebook_mapping = Dict()
#-------------------------------------------------------------------------
# Methods for managing kernels and sessions
#-------------------------------------------------------------------------
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 def kernel_for_notebook(self, notebook_id):
"""Return the kernel_id for a notebook_id or None."""
return self._notebook_mapping.get(notebook_id)
def set_kernel_for_notebook(self, notebook_id, kernel_id):
"""Associate a notebook with a kernel."""
if notebook_id is not None:
self._notebook_mapping[notebook_id] = kernel_id
Brian E. Granger
Adding kernel/notebook associations.
r4494 def notebook_for_kernel(self, kernel_id):
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 """Return the notebook_id for a kernel_id or None."""
Brian E. Granger
Adding kernel/notebook associations.
r4494 notebook_ids = [k for k, v in self._notebook_mapping.iteritems() if v == kernel_id]
if len(notebook_ids) == 1:
return notebook_ids[0]
else:
return None
def delete_mapping_for_kernel(self, kernel_id):
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 """Remove the kernel/notebook mapping for kernel_id."""
Brian E. Granger
Adding kernel/notebook associations.
r4494 notebook_id = self.notebook_for_kernel(kernel_id)
if notebook_id is not None:
del self._notebook_mapping[notebook_id]
MinRK
use notebook-dir as cwd for kernels
r7558 def start_kernel(self, notebook_id=None, **kwargs):
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 """Start a kernel for a notebok an return its kernel_id.
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495
Parameters
----------
notebook_id : uuid
The uuid of the notebook to associate the new kernel with. If this
is not None, this kernel will be persistent whenever the notebook
requests a kernel.
"""
kernel_id = self.kernel_for_notebook(notebook_id)
Brian E. Granger
Adding kernel/notebook associations.
r4494 if kernel_id is None:
kwargs['extra_arguments'] = self.kernel_argv
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs)
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 self.set_kernel_for_notebook(notebook_id, kernel_id)
self.log.info("Kernel started: %s" % kernel_id)
Brian E. Granger
Adding kernel/notebook associations.
r4494 self.log.debug("Kernel args: %r" % kwargs)
else:
self.log.info("Using existing kernel: %s" % kernel_id)
return kernel_id
Brian E. Granger
Make KernelManager.kill_kernel private....
r9130 def shutdown_kernel(self, kernel_id, now=False):
MinRK
use shutdown_kernel instead of hard kill in notebook
r7627 """Shutdown a kernel and remove its notebook association."""
self._check_kernel_id(kernel_id)
Brian E. Granger
Make KernelManager.kill_kernel private....
r9130 super(MappingKernelManager, self).shutdown_kernel(
kernel_id, now=now
)
MinRK
use shutdown_kernel instead of hard kill in notebook
r7627 self.delete_mapping_for_kernel(kernel_id)
self.log.info("Kernel shutdown: %s" % kernel_id)
Brian E. Granger
Adding kernel/notebook associations.
r4494 def interrupt_kernel(self, kernel_id):
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 """Interrupt a kernel."""
Brian E. Granger
Adding messages to HTTPError raising....
r4676 self._check_kernel_id(kernel_id)
Brian E. Granger
Major refactor of kernel connection management in the notebook....
r4545 super(MappingKernelManager, self).interrupt_kernel(kernel_id)
self.log.info("Kernel interrupted: %s" % kernel_id)
Brian E. Granger
Adding kernel/notebook associations.
r4494
def restart_kernel(self, kernel_id):
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495 """Restart a kernel while keeping clients connected."""
Brian E. Granger
Adding messages to HTTPError raising....
r4676 self._check_kernel_id(kernel_id)
Brian E. Granger
Removing unused code in the notebook MappingKernelManager....
r9033 super(MappingKernelManager, self).restart_kernel(kernel_id)
MinRK
use zmq.KernelManager to manage individual kernels in notebook...
r4960 self.log.info("Kernel restarted: %s" % kernel_id)
Brian E. Granger
Adding kernel/notebook associations.
r4494
Brian E. Granger
WebSocket url is now passed to browser when a kernel is started.
r4572 def create_iopub_stream(self, kernel_id):
Brian E. Granger
Adding messages to HTTPError raising....
r4676 """Create a new iopub stream."""
self._check_kernel_id(kernel_id)
Brian E. Granger
WebSocket url is now passed to browser when a kernel is started.
r4572 return super(MappingKernelManager, self).create_iopub_stream(kernel_id)
def create_shell_stream(self, kernel_id):
Brian E. Granger
Adding messages to HTTPError raising....
r4676 """Create a new shell stream."""
self._check_kernel_id(kernel_id)
Brian E. Granger
WebSocket url is now passed to browser when a kernel is started.
r4572 return super(MappingKernelManager, self).create_shell_stream(kernel_id)
def create_hb_stream(self, kernel_id):
Brian E. Granger
Adding messages to HTTPError raising....
r4676 """Create a new hb stream."""
self._check_kernel_id(kernel_id)
Brian E. Granger
WebSocket url is now passed to browser when a kernel is started.
r4572 return super(MappingKernelManager, self).create_hb_stream(kernel_id)
Brian E. Granger
Fixed subtle bug in kernel restarting....
r4495
Brian E. Granger
Adding messages to HTTPError raising....
r4676 def _check_kernel_id(self, kernel_id):
"""Check a that a kernel_id exists and raise 404 if not."""
if kernel_id not in self:
raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id)