manager.py
77 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
MinRK
|
r10298 | """A kernel manager for in-process kernels.""" | ||
epatters
|
r8408 | |||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r10298 | # Copyright (C) 2013 The IPython Development Team | ||
epatters
|
r8408 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r10304 | from IPython.utils.traitlets import Instance, DottedObjectName | ||
MinRK
|
r10298 | from IPython.kernel.managerabc import KernelManagerABC | ||
from IPython.kernel.manager import KernelManager | ||||
epatters
|
r8408 | |||
#----------------------------------------------------------------------------- | ||||
# Main kernel manager class | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r10298 | class InProcessKernelManager(KernelManager): | ||
Brian Granger
|
r9128 | """A manager for an in-process kernel. | ||
epatters
|
r8408 | |||
Brian Granger
|
r9128 | This class implements the interface of | ||
MinRK
|
r9354 | `IPython.kernel.kernelmanagerabc.KernelManagerABC` and allows | ||
Brian Granger
|
r9128 | (asynchronous) frontends to be used seamlessly with an in-process kernel. | ||
MinRK
|
r10297 | |||
MinRK
|
r9370 | See `IPython.kernel.kernelmanager.KernelManager` for docstrings. | ||
epatters
|
r8408 | """ | ||
# The kernel process with which the KernelManager is communicating. | ||||
MinRK
|
r9375 | kernel = Instance('IPython.kernel.inprocess.ipkernel.InProcessKernel') | ||
MinRK
|
r10304 | # the client class for KM.client() shortcut | ||
client_class = DottedObjectName('IPython.kernel.inprocess.BlockingInProcessKernelClient') | ||||
epatters
|
r8408 | |||
#-------------------------------------------------------------------------- | ||||
MinRK
|
r10298 | # Kernel management methods | ||
epatters
|
r8408 | #-------------------------------------------------------------------------- | ||
MinRK
|
r10297 | |||
epatters
|
r8408 | def start_kernel(self, **kwds): | ||
MinRK
|
r9375 | from IPython.kernel.inprocess.ipkernel import InProcessKernel | ||
epatters
|
r8471 | self.kernel = InProcessKernel() | ||
epatters
|
r8408 | |||
def shutdown_kernel(self): | ||||
Brian E. Granger
|
r9130 | self._kill_kernel() | ||
epatters
|
r8408 | |||
def restart_kernel(self, now=False, **kwds): | ||||
self.shutdown_kernel() | ||||
self.start_kernel(**kwds) | ||||
@property | ||||
def has_kernel(self): | ||||
return self.kernel is not None | ||||
Brian E. Granger
|
r9130 | def _kill_kernel(self): | ||
epatters
|
r8408 | self.kernel = None | ||
def interrupt_kernel(self): | ||||
epatters
|
r8471 | raise NotImplementedError("Cannot interrupt in-process kernel.") | ||
epatters
|
r8408 | |||
def signal_kernel(self, signum): | ||||
epatters
|
r8471 | raise NotImplementedError("Cannot signal in-process kernel.") | ||
epatters
|
r8408 | |||
def is_alive(self): | ||||
MinRK
|
r10304 | return self.kernel is not None | ||
def client(self, **kwargs): | ||||
kwargs['kernel'] = self.kernel | ||||
return super(InProcessKernelManager, self).client(**kwargs) | ||||
epatters
|
r8408 | |||
Brian Granger
|
r9121 | #----------------------------------------------------------------------------- | ||
# ABC Registration | ||||
#----------------------------------------------------------------------------- | ||||
epatters
|
r8408 | |||
Brian Granger
|
r9121 | KernelManagerABC.register(InProcessKernelManager) | ||