kernelmanager.py
240 lines
| 8.2 KiB
| text/x-python
|
PythonLexer
epatters
|
r2643 | """ Defines a KernelManager that provides signals and slots. | ||
epatters
|
r2609 | """ | ||
# System library imports. | ||||
from PyQt4 import QtCore | ||||
# IPython imports. | ||||
Brian Granger
|
r2786 | from IPython.utils.traitlets import Type | ||
epatters
|
r2611 | from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \ | ||
Brian Granger
|
r2910 | XReqSocketChannel, RepSocketChannel, HBSocketChannel | ||
epatters
|
r2994 | from util import MetaQObjectHasTraits, SuperQObject | ||
epatters
|
r2609 | |||
Brian Granger
|
r2695 | |||
epatters
|
r2994 | class SocketChannelQObject(SuperQObject): | ||
# Emitted when the channel is started. | ||||
started = QtCore.pyqtSignal() | ||||
# Emitted when the channel is stopped. | ||||
stopped = QtCore.pyqtSignal() | ||||
#--------------------------------------------------------------------------- | ||||
# 'ZmqSocketChannel' interface | ||||
#--------------------------------------------------------------------------- | ||||
def start(self): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
super(SocketChannelQObject, self).start() | ||||
self.started.emit() | ||||
def stop(self): | ||||
""" Reimplemented to emit signal. | ||||
""" | ||||
super(SocketChannelQObject, self).stop() | ||||
self.stopped.emit() | ||||
class QtXReqSocketChannel(SocketChannelQObject, XReqSocketChannel): | ||||
# Emitted when any message is received. | ||||
message_received = QtCore.pyqtSignal(object) | ||||
Brian Granger
|
r3024 | # Emitted when a reply has been received for the corresponding request | ||
# type. | ||||
epatters
|
r2994 | execute_reply = QtCore.pyqtSignal(object) | ||
complete_reply = QtCore.pyqtSignal(object) | ||||
object_info_reply = QtCore.pyqtSignal(object) | ||||
epatters
|
r3032 | # Emitted when the first reply comes back. | ||
Brian Granger
|
r3025 | first_reply = QtCore.pyqtSignal() | ||
Brian Granger
|
r3024 | |||
# Used by the first_reply signal logic to determine if a reply is the | ||||
# first. | ||||
_handlers_called = False | ||||
epatters
|
r2994 | #--------------------------------------------------------------------------- | ||
# 'XReqSocketChannel' interface | ||||
#--------------------------------------------------------------------------- | ||||
def call_handlers(self, msg): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
# Emit signals for specialized message types. | ||||
msg_type = msg['msg_type'] | ||||
signal = getattr(self, msg_type, None) | ||||
if signal: | ||||
signal.emit(msg) | ||||
Brian Granger
|
r3024 | if not self._handlers_called: | ||
self.first_reply.emit() | ||||
epatters
|
r3032 | self._handlers_called = True | ||
Brian Granger
|
r3024 | |||
epatters
|
r3032 | #--------------------------------------------------------------------------- | ||
# 'QtXReqSocketChannel' interface | ||||
#--------------------------------------------------------------------------- | ||||
Brian Granger
|
r3024 | |||
def reset_first_reply(self): | ||||
""" Reset the first_reply signal to fire again on the next reply. | ||||
""" | ||||
self._handlers_called = False | ||||
epatters
|
r2994 | |||
class QtSubSocketChannel(SocketChannelQObject, SubSocketChannel): | ||||
epatters
|
r2609 | |||
# Emitted when any message is received. | ||||
epatters
|
r2611 | message_received = QtCore.pyqtSignal(object) | ||
epatters
|
r2609 | |||
epatters
|
r2770 | # Emitted when a message of type 'stream' is received. | ||
stream_received = QtCore.pyqtSignal(object) | ||||
epatters
|
r2609 | |||
epatters
|
r2770 | # Emitted when a message of type 'pyin' is received. | ||
pyin_received = QtCore.pyqtSignal(object) | ||||
# Emitted when a message of type 'pyout' is received. | ||||
pyout_received = QtCore.pyqtSignal(object) | ||||
# Emitted when a message of type 'pyerr' is received. | ||||
pyerr_received = QtCore.pyqtSignal(object) | ||||
# Emitted when a crash report message is received from the kernel's | ||||
# last-resort sys.excepthook. | ||||
crash_received = QtCore.pyqtSignal(object) | ||||
epatters
|
r2611 | |||
MinRK
|
r3090 | # Emitted when a shutdown is noticed. | ||
shutdown_reply_received = QtCore.pyqtSignal(object) | ||||
epatters
|
r2611 | #--------------------------------------------------------------------------- | ||
epatters
|
r2609 | # 'SubSocketChannel' interface | ||
#--------------------------------------------------------------------------- | ||||
def call_handlers(self, msg): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
# Emit signals for specialized message types. | ||||
msg_type = msg['msg_type'] | ||||
epatters
|
r2770 | signal = getattr(self, msg_type + '_received', None) | ||
if signal: | ||||
signal.emit(msg) | ||||
elif msg_type in ('stdout', 'stderr'): | ||||
self.stream_received.emit(msg) | ||||
epatters
|
r2609 | |||
epatters
|
r2614 | def flush(self): | ||
""" Reimplemented to ensure that signals are dispatched immediately. | ||||
""" | ||||
super(QtSubSocketChannel, self).flush() | ||||
QtCore.QCoreApplication.instance().processEvents() | ||||
epatters
|
r2609 | |||
epatters
|
r2994 | class QtRepSocketChannel(SocketChannelQObject, RepSocketChannel): | ||
epatters
|
r2609 | |||
epatters
|
r2707 | # Emitted when any message is received. | ||
message_received = QtCore.pyqtSignal(object) | ||||
epatters
|
r2730 | # Emitted when an input request is received. | ||
input_requested = QtCore.pyqtSignal(object) | ||||
epatters
|
r2707 | |||
epatters
|
r2611 | #--------------------------------------------------------------------------- | ||
epatters
|
r2707 | # 'RepSocketChannel' interface | ||
#--------------------------------------------------------------------------- | ||||
def call_handlers(self, msg): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.message_received.emit(msg) | ||||
# Emit signals for specialized message types. | ||||
msg_type = msg['msg_type'] | ||||
epatters
|
r2730 | if msg_type == 'input_request': | ||
self.input_requested.emit(msg) | ||||
epatters
|
r2611 | |||
epatters
|
r2770 | |||
epatters
|
r2994 | class QtHBSocketChannel(SocketChannelQObject, HBSocketChannel): | ||
Brian Granger
|
r2910 | |||
# Emitted when the kernel has died. | ||||
kernel_died = QtCore.pyqtSignal(object) | ||||
#--------------------------------------------------------------------------- | ||||
epatters
|
r2994 | # 'HBSocketChannel' interface | ||
Brian Granger
|
r2910 | #--------------------------------------------------------------------------- | ||
def call_handlers(self, since_last_heartbeat): | ||||
""" Reimplemented to emit signals instead of making callbacks. | ||||
""" | ||||
# Emit the generic signal. | ||||
self.kernel_died.emit(since_last_heartbeat) | ||||
epatters
|
r2994 | class QtKernelManager(KernelManager, SuperQObject): | ||
epatters
|
r2643 | """ A KernelManager that provides signals and slots. | ||
epatters
|
r2611 | """ | ||
epatters
|
r2643 | __metaclass__ = MetaQObjectHasTraits | ||
# Emitted when the kernel manager has started listening. | ||||
epatters
|
r2701 | started_channels = QtCore.pyqtSignal() | ||
epatters
|
r2643 | |||
# Emitted when the kernel manager has stopped listening. | ||||
epatters
|
r2701 | stopped_channels = QtCore.pyqtSignal() | ||
epatters
|
r2643 | |||
# Use Qt-specific channel classes that emit signals. | ||||
Brian Granger
|
r2786 | sub_channel_class = Type(QtSubSocketChannel) | ||
xreq_channel_class = Type(QtXReqSocketChannel) | ||||
rep_channel_class = Type(QtRepSocketChannel) | ||||
Brian Granger
|
r2910 | hb_channel_class = Type(QtHBSocketChannel) | ||
epatters
|
r2643 | |||
#--------------------------------------------------------------------------- | ||||
# 'KernelManager' interface | ||||
#--------------------------------------------------------------------------- | ||||
epatters
|
r3032 | |||
#------ Kernel process management ------------------------------------------ | ||||
def start_kernel(self, *args, **kw): | ||||
""" Reimplemented for proper heartbeat management. | ||||
""" | ||||
if self._xreq_channel is not None: | ||||
self._xreq_channel.reset_first_reply() | ||||
super(QtKernelManager, self).start_kernel(*args, **kw) | ||||
#------ Channel management ------------------------------------------------- | ||||
epatters
|
r2643 | |||
epatters
|
r2994 | def start_channels(self, *args, **kw): | ||
epatters
|
r2643 | """ Reimplemented to emit signal. | ||
""" | ||||
epatters
|
r2994 | super(QtKernelManager, self).start_channels(*args, **kw) | ||
epatters
|
r2701 | self.started_channels.emit() | ||
epatters
|
r2643 | |||
Brian Granger
|
r2699 | def stop_channels(self): | ||
epatters
|
r2643 | """ Reimplemented to emit signal. | ||
""" | ||||
Brian Granger
|
r2699 | super(QtKernelManager, self).stop_channels() | ||
epatters
|
r2701 | self.stopped_channels.emit() | ||
epatters
|
r3032 | |||
@property | ||||
def xreq_channel(self): | ||||
""" Reimplemented for proper heartbeat management. | ||||
""" | ||||
if self._xreq_channel is None: | ||||
self._xreq_channel = super(QtKernelManager, self).xreq_channel | ||||
self._xreq_channel.first_reply.connect(self._first_reply) | ||||
return self._xreq_channel | ||||
#--------------------------------------------------------------------------- | ||||
# Protected interface | ||||
#--------------------------------------------------------------------------- | ||||
def _first_reply(self): | ||||
""" Unpauses the heartbeat channel when the first reply is received on | ||||
the execute channel. Note that this will *not* start the heartbeat | ||||
channel if it is not already running! | ||||
""" | ||||
if self._hb_channel is not None: | ||||
self._hb_channel.unpause() | ||||