/BDDS_dnaCompleteExome_optimized/pymodules/python2.7/lib/python/ipython-2.2.0-py2.7.egg/IPython/kernel/client.py

https://gitlab.com/pooja043/Globus_Docker · Python · 206 lines · 108 code · 13 blank · 85 comment · 13 complexity · 8cec8004aa7373e717576e7b54ba6082 MD5 · raw file

  1. """Base class to manage the interaction with a running kernel
  2. """
  3. #-----------------------------------------------------------------------------
  4. # Copyright (C) 2013 The IPython Development Team
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #-----------------------------------------------------------------------------
  9. #-----------------------------------------------------------------------------
  10. # Imports
  11. #-----------------------------------------------------------------------------
  12. from __future__ import absolute_import
  13. import zmq
  14. # Local imports
  15. from IPython.config.configurable import LoggingConfigurable
  16. from IPython.utils.traitlets import (
  17. Any, Instance, Type,
  18. )
  19. from .zmq.session import Session
  20. from .channels import (
  21. ShellChannel, IOPubChannel,
  22. HBChannel, StdInChannel,
  23. )
  24. from .clientabc import KernelClientABC
  25. from .connect import ConnectionFileMixin
  26. #-----------------------------------------------------------------------------
  27. # Main kernel client class
  28. #-----------------------------------------------------------------------------
  29. class KernelClient(LoggingConfigurable, ConnectionFileMixin):
  30. """Communicates with a single kernel on any host via zmq channels.
  31. There are four channels associated with each kernel:
  32. * shell: for request/reply calls to the kernel.
  33. * iopub: for the kernel to publish results to frontends.
  34. * hb: for monitoring the kernel's heartbeat.
  35. * stdin: for frontends to reply to raw_input calls in the kernel.
  36. The methods of the channels are exposed as methods of the client itself
  37. (KernelClient.execute, complete, history, etc.).
  38. See the channels themselves for documentation of these methods.
  39. """
  40. # The PyZMQ Context to use for communication with the kernel.
  41. context = Instance(zmq.Context)
  42. def _context_default(self):
  43. return zmq.Context.instance()
  44. # The Session to use for communication with the kernel.
  45. session = Instance(Session)
  46. def _session_default(self):
  47. return Session(parent=self)
  48. # The classes to use for the various channels
  49. shell_channel_class = Type(ShellChannel)
  50. iopub_channel_class = Type(IOPubChannel)
  51. stdin_channel_class = Type(StdInChannel)
  52. hb_channel_class = Type(HBChannel)
  53. # Protected traits
  54. _shell_channel = Any
  55. _iopub_channel = Any
  56. _stdin_channel = Any
  57. _hb_channel = Any
  58. #--------------------------------------------------------------------------
  59. # Channel proxy methods
  60. #--------------------------------------------------------------------------
  61. def _get_msg(channel, *args, **kwargs):
  62. return channel.get_msg(*args, **kwargs)
  63. def get_shell_msg(self, *args, **kwargs):
  64. """Get a message from the shell channel"""
  65. return self.shell_channel.get_msg(*args, **kwargs)
  66. def get_iopub_msg(self, *args, **kwargs):
  67. """Get a message from the iopub channel"""
  68. return self.iopub_channel.get_msg(*args, **kwargs)
  69. def get_stdin_msg(self, *args, **kwargs):
  70. """Get a message from the stdin channel"""
  71. return self.stdin_channel.get_msg(*args, **kwargs)
  72. #--------------------------------------------------------------------------
  73. # Channel management methods
  74. #--------------------------------------------------------------------------
  75. def start_channels(self, shell=True, iopub=True, stdin=True, hb=True):
  76. """Starts the channels for this kernel.
  77. This will create the channels if they do not exist and then start
  78. them (their activity runs in a thread). If port numbers of 0 are
  79. being used (random ports) then you must first call
  80. :meth:`start_kernel`. If the channels have been stopped and you
  81. call this, :class:`RuntimeError` will be raised.
  82. """
  83. if shell:
  84. self.shell_channel.start()
  85. for method in self.shell_channel.proxy_methods:
  86. setattr(self, method, getattr(self.shell_channel, method))
  87. if iopub:
  88. self.iopub_channel.start()
  89. for method in self.iopub_channel.proxy_methods:
  90. setattr(self, method, getattr(self.iopub_channel, method))
  91. if stdin:
  92. self.stdin_channel.start()
  93. for method in self.stdin_channel.proxy_methods:
  94. setattr(self, method, getattr(self.stdin_channel, method))
  95. self.shell_channel.allow_stdin = True
  96. else:
  97. self.shell_channel.allow_stdin = False
  98. if hb:
  99. self.hb_channel.start()
  100. def stop_channels(self):
  101. """Stops all the running channels for this kernel.
  102. This stops their event loops and joins their threads.
  103. """
  104. if self.shell_channel.is_alive():
  105. self.shell_channel.stop()
  106. if self.iopub_channel.is_alive():
  107. self.iopub_channel.stop()
  108. if self.stdin_channel.is_alive():
  109. self.stdin_channel.stop()
  110. if self.hb_channel.is_alive():
  111. self.hb_channel.stop()
  112. @property
  113. def channels_running(self):
  114. """Are any of the channels created and running?"""
  115. return (self.shell_channel.is_alive() or self.iopub_channel.is_alive() or
  116. self.stdin_channel.is_alive() or self.hb_channel.is_alive())
  117. @property
  118. def shell_channel(self):
  119. """Get the shell channel object for this kernel."""
  120. if self._shell_channel is None:
  121. url = self._make_url('shell')
  122. self.log.debug("connecting shell channel to %s", url)
  123. self._shell_channel = self.shell_channel_class(
  124. self.context, self.session, url
  125. )
  126. return self._shell_channel
  127. @property
  128. def iopub_channel(self):
  129. """Get the iopub channel object for this kernel."""
  130. if self._iopub_channel is None:
  131. url = self._make_url('iopub')
  132. self.log.debug("connecting iopub channel to %s", url)
  133. self._iopub_channel = self.iopub_channel_class(
  134. self.context, self.session, url
  135. )
  136. return self._iopub_channel
  137. @property
  138. def stdin_channel(self):
  139. """Get the stdin channel object for this kernel."""
  140. if self._stdin_channel is None:
  141. url = self._make_url('stdin')
  142. self.log.debug("connecting stdin channel to %s", url)
  143. self._stdin_channel = self.stdin_channel_class(
  144. self.context, self.session, url
  145. )
  146. return self._stdin_channel
  147. @property
  148. def hb_channel(self):
  149. """Get the hb channel object for this kernel."""
  150. if self._hb_channel is None:
  151. url = self._make_url('hb')
  152. self.log.debug("connecting heartbeat channel to %s", url)
  153. self._hb_channel = self.hb_channel_class(
  154. self.context, self.session, url
  155. )
  156. return self._hb_channel
  157. def is_alive(self):
  158. """Is the kernel process still running?"""
  159. if self._hb_channel is not None:
  160. # We didn't start the kernel with this KernelManager so we
  161. # use the heartbeat.
  162. return self._hb_channel.is_beating()
  163. else:
  164. # no heartbeat and not local, we can't tell if it's running,
  165. # so naively return True
  166. return True
  167. #-----------------------------------------------------------------------------
  168. # ABC Registration
  169. #-----------------------------------------------------------------------------
  170. KernelClientABC.register(KernelClient)