kernelmanager.py
98 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
MinRK
|
r9371 | """A kernel manager relating notebooks and kernels | ||
Brian E. Granger
|
r4609 | |||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
Brian E. Granger
|
r4343 | |||
Brian E. Granger
|
r4344 | #----------------------------------------------------------------------------- | ||
MinRK
|
r9371 | # Copyright (C) 2013 The IPython Development Team | ||
Brian E. Granger
|
r4348 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
Brian E. Granger
|
r4609 | # the file COPYING, distributed as part of this software. | ||
Brian E. Granger
|
r4348 | #----------------------------------------------------------------------------- | ||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4344 | # Imports | ||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4494 | from tornado import web | ||
MinRK
|
r9371 | from IPython.kernel.multikernelmanager import MultiKernelManager | ||
MinRK
|
r6317 | from IPython.utils.traitlets import ( | ||
Thomas Kluyver
|
r11129 | Dict, List, Unicode, | ||
MinRK
|
r6317 | ) | ||
Brian E. Granger
|
r10273 | |||
Brian E. Granger
|
r4344 | #----------------------------------------------------------------------------- | ||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r4297 | |||
Brian E. Granger
|
r4545 | |||
MinRK
|
r4960 | class MappingKernelManager(MultiKernelManager): | ||
MinRK
|
r10355 | """A KernelManager that handles notebook mapping and HTTP error handling""" | ||
Brian E. Granger
|
r4494 | |||
MinRK
|
r10295 | def _kernel_manager_class_default(self): | ||
return "IPython.kernel.ioloop.IOLoopKernelManager" | ||||
Brian E. Granger
|
r4494 | kernel_argv = List(Unicode) | ||
#------------------------------------------------------------------------- | ||||
# Methods for managing kernels and sessions | ||||
#------------------------------------------------------------------------- | ||||
MinRK
|
r10321 | def _handle_kernel_died(self, kernel_id): | ||
"""notice that a kernel died""" | ||||
self.log.warn("Kernel %s died, removing from map.", kernel_id) | ||||
Thomas Kluyver
|
r11074 | self.remove_kernel(kernel_id) | ||
MinRK
|
r10321 | |||
Zachary Sailer
|
r13035 | def start_kernel(self, kernel_id=None, **kwargs): | ||
Zachary Sailer
|
r12983 | """Start a kernel for a session an return its kernel_id. | ||
Brian E. Granger
|
r4495 | |||
Parameters | ||||
---------- | ||||
Zachary Sailer
|
r13035 | kernel_id : uuid | ||
The uuid to associate the new kernel with. If this | ||||
is not None, this kernel will be persistent whenever it is | ||||
requested. | ||||
Brian E. Granger
|
r4495 | """ | ||
Brian E. Granger
|
r4494 | if kernel_id is None: | ||
kwargs['extra_arguments'] = self.kernel_argv | ||||
Brian E. Granger
|
r4545 | kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs) | ||
Brian E. Granger
|
r4495 | self.log.info("Kernel started: %s" % kernel_id) | ||
Brian E. Granger
|
r4494 | self.log.debug("Kernel args: %r" % kwargs) | ||
MinRK
|
r10321 | # register callback for failed auto-restart | ||
self.add_restart_callback(kernel_id, | ||||
lambda : self._handle_kernel_died(kernel_id), | ||||
'dead', | ||||
) | ||||
Brian E. Granger
|
r4494 | else: | ||
Zachary Sailer
|
r13058 | self._check_kernel_id(kernel_id) | ||
Brian E. Granger
|
r4494 | self.log.info("Using existing kernel: %s" % kernel_id) | ||
return kernel_id | ||||
MinRK
|
r10330 | def shutdown_kernel(self, kernel_id, now=False): | ||
"""Shutdown a kernel by kernel_id""" | ||||
Zachary Sailer
|
r13058 | self._check_kernel_id(kernel_id) | ||
MinRK
|
r10330 | super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now) | ||
Zachary Sailer
|
r12983 | |||
def kernel_model(self, kernel_id, ws_url): | ||||
Zachary Sailer
|
r13037 | """Return a dictionary of kernel information described in the | ||
JSON standard model.""" | ||||
Zachary Sailer
|
r13058 | self._check_kernel_id(kernel_id) | ||
Zachary Sailer
|
r13015 | model = {"id":kernel_id, "ws_url": ws_url} | ||
Zachary Sailer
|
r12983 | return model | ||
Zachary Sailer
|
r13052 | def list_kernels(self, ws_url): | ||
Zachary Sailer
|
r13037 | """Returns a list of kernel_id's of kernels running.""" | ||
Zachary Sailer
|
r13052 | kernels = [] | ||
kernel_ids = super(MappingKernelManager, self).list_kernel_ids() | ||||
for kernel_id in kernel_ids: | ||||
model = self.kernel_model(kernel_id, ws_url) | ||||
kernels.append(model) | ||||
return kernels | ||||
MinRK
|
r10330 | |||
MinRK
|
r10295 | # override _check_kernel_id to raise 404 instead of KeyError | ||
Brian E. Granger
|
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) | ||||