manager.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """A kernel manager for in-process kernels."""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from jupyter_client.manager import KernelManager
  5. from jupyter_client.managerabc import KernelManagerABC
  6. from jupyter_client.session import Session
  7. from traitlets import DottedObjectName, Instance, default
  8. from .constants import INPROCESS_KEY
  9. class InProcessKernelManager(KernelManager):
  10. """A manager for an in-process kernel.
  11. This class implements the interface of
  12. `jupyter_client.kernelmanagerabc.KernelManagerABC` and allows
  13. (asynchronous) frontends to be used seamlessly with an in-process kernel.
  14. See `jupyter_client.kernelmanager.KernelManager` for docstrings.
  15. """
  16. # The kernel process with which the KernelManager is communicating.
  17. kernel = Instance("ipykernel.inprocess.ipkernel.InProcessKernel", allow_none=True)
  18. # the client class for KM.client() shortcut
  19. client_class = DottedObjectName("ipykernel.inprocess.BlockingInProcessKernelClient")
  20. @default("blocking_class")
  21. def _default_blocking_class(self):
  22. from .blocking import BlockingInProcessKernelClient
  23. return BlockingInProcessKernelClient
  24. @default("session")
  25. def _default_session(self):
  26. # don't sign in-process messages
  27. return Session(key=INPROCESS_KEY, parent=self)
  28. # --------------------------------------------------------------------------
  29. # Kernel management methods
  30. # --------------------------------------------------------------------------
  31. def start_kernel(self, **kwds):
  32. """Start the kernel."""
  33. from ipykernel.inprocess.ipkernel import InProcessKernel
  34. self.kernel = InProcessKernel(parent=self, session=self.session)
  35. def shutdown_kernel(self):
  36. """Shutdown the kernel."""
  37. if self.kernel:
  38. self.kernel.iopub_thread.stop()
  39. self._kill_kernel()
  40. def restart_kernel(self, now=False, **kwds):
  41. """Restart the kernel."""
  42. self.shutdown_kernel()
  43. self.start_kernel(**kwds)
  44. @property
  45. def has_kernel(self):
  46. return self.kernel is not None
  47. def _kill_kernel(self):
  48. self.kernel = None
  49. def interrupt_kernel(self):
  50. """Interrupt the kernel."""
  51. msg = "Cannot interrupt in-process kernel."
  52. raise NotImplementedError(msg)
  53. def signal_kernel(self, signum):
  54. """Send a signal to the kernel."""
  55. msg = "Cannot signal in-process kernel."
  56. raise NotImplementedError(msg)
  57. def is_alive(self):
  58. """Test if the kernel is alive."""
  59. return self.kernel is not None
  60. def client(self, **kwargs):
  61. """Get a client for the kernel."""
  62. kwargs["kernel"] = self.kernel
  63. return super().client(**kwargs)
  64. # -----------------------------------------------------------------------------
  65. # ABC Registration
  66. # -----------------------------------------------------------------------------
  67. KernelManagerABC.register(InProcessKernelManager)