client.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """ Defines a KernelClient that provides signals and slots.
  2. """
  3. import atexit
  4. import errno
  5. from threading import Thread
  6. import time
  7. import zmq
  8. # import ZMQError in top-level namespace, to avoid ugly attribute-error messages
  9. # during garbage collection of threads at exit:
  10. from zmq import ZMQError
  11. from zmq.eventloop import ioloop, zmqstream
  12. from qtpy import QtCore
  13. # Local imports
  14. from traitlets import Type, Instance
  15. from jupyter_client.channels import HBChannel
  16. from jupyter_client import KernelClient
  17. from jupyter_client.channels import InvalidPortNumber
  18. from jupyter_client.threaded import ThreadedKernelClient, ThreadedZMQSocketChannel
  19. from .kernel_mixins import QtKernelClientMixin
  20. from .util import SuperQObject
  21. class QtHBChannel(SuperQObject, HBChannel):
  22. # A longer timeout than the base class
  23. time_to_dead = 3.0
  24. # Emitted when the kernel has died.
  25. kernel_died = QtCore.Signal(object)
  26. def call_handlers(self, since_last_heartbeat):
  27. """ Reimplemented to emit signals instead of making callbacks.
  28. """
  29. # Emit the generic signal.
  30. self.kernel_died.emit(since_last_heartbeat)
  31. from jupyter_client import protocol_version_info
  32. major_protocol_version = protocol_version_info[0]
  33. class QtZMQSocketChannel(ThreadedZMQSocketChannel,SuperQObject):
  34. """A ZMQ socket emitting a Qt signal when a message is received."""
  35. message_received = QtCore.Signal(object)
  36. def process_events(self):
  37. """ Process any pending GUI events.
  38. """
  39. QtCore.QCoreApplication.instance().processEvents()
  40. def call_handlers(self, msg):
  41. """This method is called in the ioloop thread when a message arrives.
  42. It is important to remember that this method is called in the thread
  43. so that some logic must be done to ensure that the application level
  44. handlers are called in the application thread.
  45. """
  46. # Emit the generic signal.
  47. self.message_received.emit(msg)
  48. class QtKernelClient(QtKernelClientMixin, ThreadedKernelClient):
  49. """ A KernelClient that provides signals and slots.
  50. """
  51. iopub_channel_class = Type(QtZMQSocketChannel)
  52. shell_channel_class = Type(QtZMQSocketChannel)
  53. stdin_channel_class = Type(QtZMQSocketChannel)
  54. hb_channel_class = Type(QtHBChannel)