manager.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """A kernel manager with a tornado IOLoop"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import typing as t
  5. import zmq
  6. from tornado import ioloop
  7. from traitlets import Instance, Type
  8. from zmq.eventloop.zmqstream import ZMQStream
  9. from ..manager import AsyncKernelManager, KernelManager
  10. from .restarter import AsyncIOLoopKernelRestarter, IOLoopKernelRestarter
  11. def as_zmqstream(f: t.Any) -> t.Callable:
  12. """Convert a socket to a zmq stream."""
  13. def wrapped(self: t.Any, *args: t.Any, **kwargs: t.Any) -> t.Any:
  14. save_socket_class = None
  15. # zmqstreams only support sync sockets
  16. if self.context._socket_class is not zmq.Socket:
  17. save_socket_class = self.context._socket_class
  18. self.context._socket_class = zmq.Socket
  19. try:
  20. socket = f(self, *args, **kwargs)
  21. finally:
  22. if save_socket_class:
  23. # restore default socket class
  24. self.context._socket_class = save_socket_class
  25. return ZMQStream(socket, self.loop)
  26. return wrapped
  27. class IOLoopKernelManager(KernelManager):
  28. """An io loop kernel manager."""
  29. loop = Instance("tornado.ioloop.IOLoop")
  30. def _loop_default(self) -> ioloop.IOLoop:
  31. return ioloop.IOLoop.current()
  32. restarter_class = Type(
  33. default_value=IOLoopKernelRestarter,
  34. klass=IOLoopKernelRestarter,
  35. help=(
  36. "Type of KernelRestarter to use. "
  37. "Must be a subclass of IOLoopKernelRestarter.\n"
  38. "Override this to customize how kernel restarts are managed."
  39. ),
  40. config=True,
  41. )
  42. _restarter: t.Any = Instance("jupyter_client.ioloop.IOLoopKernelRestarter", allow_none=True)
  43. def start_restarter(self) -> None:
  44. """Start the restarter."""
  45. if self.autorestart and self.has_kernel:
  46. if self._restarter is None:
  47. self._restarter = self.restarter_class(
  48. kernel_manager=self, loop=self.loop, parent=self, log=self.log
  49. )
  50. self._restarter.start()
  51. def stop_restarter(self) -> None:
  52. """Stop the restarter."""
  53. if self.autorestart and self._restarter is not None:
  54. self._restarter.stop()
  55. connect_shell = as_zmqstream(KernelManager.connect_shell)
  56. connect_control = as_zmqstream(KernelManager.connect_control)
  57. connect_iopub = as_zmqstream(KernelManager.connect_iopub)
  58. connect_stdin = as_zmqstream(KernelManager.connect_stdin)
  59. connect_hb = as_zmqstream(KernelManager.connect_hb)
  60. class AsyncIOLoopKernelManager(AsyncKernelManager):
  61. """An async ioloop kernel manager."""
  62. loop = Instance("tornado.ioloop.IOLoop")
  63. def _loop_default(self) -> ioloop.IOLoop:
  64. return ioloop.IOLoop.current()
  65. restarter_class = Type(
  66. default_value=AsyncIOLoopKernelRestarter,
  67. klass=AsyncIOLoopKernelRestarter,
  68. help=(
  69. "Type of KernelRestarter to use. "
  70. "Must be a subclass of AsyncIOLoopKernelManager.\n"
  71. "Override this to customize how kernel restarts are managed."
  72. ),
  73. config=True,
  74. )
  75. _restarter: t.Any = Instance(
  76. "jupyter_client.ioloop.AsyncIOLoopKernelRestarter", allow_none=True
  77. )
  78. def start_restarter(self) -> None:
  79. """Start the restarter."""
  80. if self.autorestart and self.has_kernel:
  81. if self._restarter is None:
  82. self._restarter = self.restarter_class(
  83. kernel_manager=self, loop=self.loop, parent=self, log=self.log
  84. )
  85. self._restarter.start()
  86. def stop_restarter(self) -> None:
  87. """Stop the restarter."""
  88. if self.autorestart and self._restarter is not None:
  89. self._restarter.stop()
  90. connect_shell = as_zmqstream(AsyncKernelManager.connect_shell)
  91. connect_control = as_zmqstream(AsyncKernelManager.connect_control)
  92. connect_iopub = as_zmqstream(AsyncKernelManager.connect_iopub)
  93. connect_stdin = as_zmqstream(AsyncKernelManager.connect_stdin)
  94. connect_hb = as_zmqstream(AsyncKernelManager.connect_hb)