manager.py
71 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
MinRK
|
r10298 | """A kernel manager for in-process kernels.""" | ||
epatters
|
r8408 | |||
Min RK
|
r20572 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
epatters
|
r8408 | |||
MinRK
|
r10304 | from IPython.utils.traitlets import Instance, DottedObjectName | ||
MinRK
|
r10298 | from IPython.kernel.managerabc import KernelManagerABC | ||
from IPython.kernel.manager import KernelManager | ||||
Min RK
|
r20572 | from IPython.kernel.zmq.session import Session | ||
epatters
|
r8408 | |||
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') | ||||
Min RK
|
r20572 | |||
def _session_default(self): | ||||
# don't sign in-process messages | ||||
return Session(key=b'', parent=self) | ||||
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 | ||
Min RK
|
r20572 | self.kernel = InProcessKernel(parent=self, session=self.session) | ||
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) | ||