channels.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """A kernel client for in-process kernels."""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from jupyter_client.channelsabc import HBChannelABC
  5. # -----------------------------------------------------------------------------
  6. # Channel classes
  7. # -----------------------------------------------------------------------------
  8. class InProcessChannel:
  9. """Base class for in-process channels."""
  10. proxy_methods: list[object] = []
  11. def __init__(self, client=None):
  12. """Initialize the channel."""
  13. super().__init__()
  14. self.client = client
  15. self._is_alive = False
  16. def is_alive(self):
  17. """Test if the channel is alive."""
  18. return self._is_alive
  19. def start(self):
  20. """Start the channel."""
  21. self._is_alive = True
  22. def stop(self):
  23. """Stop the channel."""
  24. self._is_alive = False
  25. def call_handlers(self, msg):
  26. """This method is called in the main thread when a message arrives.
  27. Subclasses should override this method to handle incoming messages.
  28. """
  29. msg = "call_handlers must be defined in a subclass."
  30. raise NotImplementedError(msg)
  31. def flush(self, timeout=1.0):
  32. """Flush the channel."""
  33. def call_handlers_later(self, *args, **kwds):
  34. """Call the message handlers later.
  35. The default implementation just calls the handlers immediately, but this
  36. method exists so that GUI toolkits can defer calling the handlers until
  37. after the event loop has run, as expected by GUI frontends.
  38. """
  39. self.call_handlers(*args, **kwds)
  40. def process_events(self):
  41. """Process any pending GUI events.
  42. This method will be never be called from a frontend without an event
  43. loop (e.g., a terminal frontend).
  44. """
  45. raise NotImplementedError
  46. class InProcessHBChannel:
  47. """A dummy heartbeat channel interface for in-process kernels.
  48. Normally we use the heartbeat to check that the kernel process is alive.
  49. When the kernel is in-process, that doesn't make sense, but clients still
  50. expect this interface.
  51. """
  52. time_to_dead = 3.0
  53. def __init__(self, client=None):
  54. """Initialize the channel."""
  55. super().__init__()
  56. self.client = client
  57. self._is_alive = False
  58. self._pause = True
  59. def is_alive(self):
  60. """Test if the channel is alive."""
  61. return self._is_alive
  62. def start(self):
  63. """Start the channel."""
  64. self._is_alive = True
  65. def stop(self):
  66. """Stop the channel."""
  67. self._is_alive = False
  68. def pause(self):
  69. """Pause the channel."""
  70. self._pause = True
  71. def unpause(self):
  72. """Unpause the channel."""
  73. self._pause = False
  74. def is_beating(self):
  75. """Test if the channel is beating."""
  76. return not self._pause
  77. HBChannelABC.register(InProcessHBChannel)