##// END OF EJS Templates
Updating main notebook template to use split scripts.
Updating main notebook template to use split scripts.

File last commit:

r4348:97f1ce7d
r4350:90a9bbce
Show More
kernelmanager.py
188 lines | 5.8 KiB | text/x-python | PythonLexer
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """A kernel manager for multiple kernels."""
Brian E. Granger
Refactoring the notebook app to support the new config system.
r4344 #-----------------------------------------------------------------------------
Brian E. Granger
Updating the notebook to work with the latex master....
r4348 # Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
Brian E. Granger
Refactoring the notebook app to support the new config system.
r4344 # Imports
#-----------------------------------------------------------------------------
Brian Granger
Work on the server side of the html notebook.
r4297 import signal
import sys
Brian Granger
Different clients now share a single zmq session....
r4306 import uuid
Brian Granger
Work on the server side of the html notebook.
r4297
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 import zmq
Brian E. Granger
Notebook app debugging....
r4345 from IPython.config.configurable import LoggingConfigurable
Brian Granger
Work on the server side of the html notebook.
r4297 from IPython.zmq.ipkernel import launch_kernel
Brian E. Granger
Work to adapt routers to new Session message protocol.
r4346 from IPython.utils.traitlets import Instance, Dict
Brian Granger
Work on the server side of the html notebook.
r4297
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 Granger
Basic server for htmlnotebook working.
r4298 class DuplicateKernelError(Exception):
pass
Brian E. Granger
Notebook app debugging....
r4345 class KernelManager(LoggingConfigurable):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """A class for managing multiple kernels."""
context = Instance('zmq.Context')
def _context_default(self):
return zmq.Context.instance()
Brian Granger
Work on the server side of the html notebook.
r4297
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 _kernels = Dict()
Brian Granger
Work on the server side of the html notebook.
r4297
@property
def kernel_ids(self):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Return a list of the kernel ids of the active kernels."""
Brian Granger
Work on the server side of the html notebook.
r4297 return self._kernels.keys()
def __len__(self):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Return the number of running kernels."""
Brian Granger
Work on the server side of the html notebook.
r4297 return len(self.kernel_ids)
def __contains__(self, kernel_id):
if kernel_id in self.kernel_ids:
return True
else:
return False
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 def start_kernel(self, **kwargs):
"""Start a new kernel."""
Brian Granger
Different clients now share a single zmq session....
r4306 kernel_id = str(uuid.uuid4())
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 (process, shell_port, iopub_port, stdin_port, hb_port) = launch_kernel(**kwargs)
# Store the information for contacting the kernel. This assumes the kernel is
# running on localhost.
Brian Granger
Work on the server side of the html notebook.
r4297 d = dict(
process = process,
stdin_port = stdin_port,
iopub_port = iopub_port,
shell_port = shell_port,
hb_port = hb_port,
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 ip = '127.0.0.1'
Brian Granger
Work on the server side of the html notebook.
r4297 )
Brian Granger
Basic server for htmlnotebook working.
r4298 self._kernels[kernel_id] = d
return kernel_id
Brian Granger
Work on the server side of the html notebook.
r4297
def kill_kernel(self, kernel_id):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Kill a kernel by its kernel uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to kill.
"""
Brian Granger
Work on the server side of the html notebook.
r4297 kernel_process = self.get_kernel_process(kernel_id)
if kernel_process is not None:
# Attempt to kill the kernel.
try:
kernel_process.kill()
except OSError, e:
# In Windows, we will get an Access Denied error if the process
# has already terminated. Ignore it.
if not (sys.platform == 'win32' and e.winerror == 5):
raise
del self._kernels[kernel_id]
def interrupt_kernel(self, kernel_id):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Interrupt (SIGINT) the kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel to interrupt.
"""
Brian Granger
Work on the server side of the html notebook.
r4297 kernel_process = self.get_kernel_process(kernel_id)
if kernel_process is not None:
if sys.platform == 'win32':
from parentpoller import ParentPollerWindows as Poller
Poller.send_interrupt(kernel_process.win32_interrupt_event)
else:
kernel_process.send_signal(signal.SIGINT)
def signal_kernel(self, kernel_id, signum):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """ Sends a signal to the kernel by its uuid.
Note that since only SIGTERM is supported on Windows, this function
is only useful on Unix systems.
Parameters
==========
kernel_id : uuid
The id of the kernel to signal.
Brian Granger
Work on the server side of the html notebook.
r4297 """
kernel_process = self.get_kernel_process(kernel_id)
if kernel_process is not None:
kernel_process.send_signal(signum)
def get_kernel_process(self, kernel_id):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Get the process object for a kernel by its uuid.
Parameters
==========
kernel_id : uuid
The id of the kernel.
"""
Brian Granger
Work on the server side of the html notebook.
r4297 d = self._kernels.get(kernel_id)
if d is not None:
return d['process']
else:
raise KeyError("Kernel with id not found: %s" % kernel_id)
def get_kernel_ports(self, kernel_id):
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 """Return a dictionary of ports for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
port_dict : dict
A dict of key, value pairs where the keys are the names
(stdin_port,iopub_port,shell_port) and the values are the
integer port numbers for those channels.
"""
Brian Granger
Work on the server side of the html notebook.
r4297 d = self._kernels.get(kernel_id)
if d is not None:
dcopy = d.copy()
dcopy.pop('process')
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 dcopy.pop('ip')
Brian Granger
Work on the server side of the html notebook.
r4297 return dcopy
else:
raise KeyError("Kernel with id not found: %s" % kernel_id)
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 def get_kernel_ip(self, kernel_id):
"""Return ip address for a kernel.
Parameters
==========
kernel_id : uuid
The id of the kernel.
Returns
=======
ip : str
The ip address of the kernel.
"""
Brian Granger
Work on the server side of the html notebook.
r4297 d = self._kernels.get(kernel_id)
if d is not None:
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 return d['ip']
Brian Granger
Work on the server side of the html notebook.
r4297 else:
raise KeyError("Kernel with id not found: %s" % kernel_id)
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 def create_session_manager(self, kernel_id):
"""Create a new session manager for a kernel by its uuid."""
from sessionmanager import SessionManager
return SessionManager(
kernel_id=kernel_id, kernel_manager=self,
Brian E. Granger
Notebook app debugging....
r4345 config=self.config, context=self.context, log=self.log
Brian E. Granger
Refactored htmlnotebook session and kernel manager....
r4343 )
Brian Granger
Work on the server side of the html notebook.
r4297