##// END OF EJS Templates
Fixing problem with first_reply signal.
Brian Granger -
Show More
@@ -1,202 +1,202 b''
1 1 """ Defines a KernelManager that provides signals and slots.
2 2 """
3 3
4 4 # System library imports.
5 5 from PyQt4 import QtCore
6 6
7 7 # IPython imports.
8 8 from IPython.utils.traitlets import Type
9 9 from IPython.zmq.kernelmanager import KernelManager, SubSocketChannel, \
10 10 XReqSocketChannel, RepSocketChannel, HBSocketChannel
11 11 from util import MetaQObjectHasTraits, SuperQObject
12 12
13 13
14 14 class SocketChannelQObject(SuperQObject):
15 15
16 16 # Emitted when the channel is started.
17 17 started = QtCore.pyqtSignal()
18 18
19 19 # Emitted when the channel is stopped.
20 20 stopped = QtCore.pyqtSignal()
21 21
22 22 #---------------------------------------------------------------------------
23 23 # 'ZmqSocketChannel' interface
24 24 #---------------------------------------------------------------------------
25 25
26 26 def start(self):
27 27 """ Reimplemented to emit signal.
28 28 """
29 29 super(SocketChannelQObject, self).start()
30 30 self.started.emit()
31 31
32 32 def stop(self):
33 33 """ Reimplemented to emit signal.
34 34 """
35 35 super(SocketChannelQObject, self).stop()
36 36 self.stopped.emit()
37 37
38 38
39 39 class QtXReqSocketChannel(SocketChannelQObject, XReqSocketChannel):
40 40
41 41 # Emitted when any message is received.
42 42 message_received = QtCore.pyqtSignal(object)
43 43
44 44 # Emitted when a reply has been received for the corresponding request
45 45 # type.
46 46 execute_reply = QtCore.pyqtSignal(object)
47 47 complete_reply = QtCore.pyqtSignal(object)
48 48 object_info_reply = QtCore.pyqtSignal(object)
49 49
50 50 # Emitted when the first reply comes back
51 first_reply = QtCore.pyqtSignal(object)
51 first_reply = QtCore.pyqtSignal()
52 52
53 53 # Used by the first_reply signal logic to determine if a reply is the
54 54 # first.
55 55 _handlers_called = False
56 56
57 57 #---------------------------------------------------------------------------
58 58 # 'XReqSocketChannel' interface
59 59 #---------------------------------------------------------------------------
60 60
61 61 def call_handlers(self, msg):
62 62 """ Reimplemented to emit signals instead of making callbacks.
63 63 """
64 64 # Emit the generic signal.
65 65 self.message_received.emit(msg)
66 66
67 67 # Emit signals for specialized message types.
68 68 msg_type = msg['msg_type']
69 69 signal = getattr(self, msg_type, None)
70 70 if signal:
71 71 signal.emit(msg)
72 72
73 73 if not self._handlers_called:
74 74 self.first_reply.emit()
75 75
76 76 self._handlers_called = True
77 77
78 78 def reset_first_reply(self):
79 79 """ Reset the first_reply signal to fire again on the next reply.
80 80 """
81 81 self._handlers_called = False
82 82
83 83
84 84 class QtSubSocketChannel(SocketChannelQObject, SubSocketChannel):
85 85
86 86 # Emitted when any message is received.
87 87 message_received = QtCore.pyqtSignal(object)
88 88
89 89 # Emitted when a message of type 'stream' is received.
90 90 stream_received = QtCore.pyqtSignal(object)
91 91
92 92 # Emitted when a message of type 'pyin' is received.
93 93 pyin_received = QtCore.pyqtSignal(object)
94 94
95 95 # Emitted when a message of type 'pyout' is received.
96 96 pyout_received = QtCore.pyqtSignal(object)
97 97
98 98 # Emitted when a message of type 'pyerr' is received.
99 99 pyerr_received = QtCore.pyqtSignal(object)
100 100
101 101 # Emitted when a crash report message is received from the kernel's
102 102 # last-resort sys.excepthook.
103 103 crash_received = QtCore.pyqtSignal(object)
104 104
105 105 #---------------------------------------------------------------------------
106 106 # 'SubSocketChannel' interface
107 107 #---------------------------------------------------------------------------
108 108
109 109 def call_handlers(self, msg):
110 110 """ Reimplemented to emit signals instead of making callbacks.
111 111 """
112 112 # Emit the generic signal.
113 113 self.message_received.emit(msg)
114 114
115 115 # Emit signals for specialized message types.
116 116 msg_type = msg['msg_type']
117 117 signal = getattr(self, msg_type + '_received', None)
118 118 if signal:
119 119 signal.emit(msg)
120 120 elif msg_type in ('stdout', 'stderr'):
121 121 self.stream_received.emit(msg)
122 122
123 123 def flush(self):
124 124 """ Reimplemented to ensure that signals are dispatched immediately.
125 125 """
126 126 super(QtSubSocketChannel, self).flush()
127 127 QtCore.QCoreApplication.instance().processEvents()
128 128
129 129
130 130 class QtRepSocketChannel(SocketChannelQObject, RepSocketChannel):
131 131
132 132 # Emitted when any message is received.
133 133 message_received = QtCore.pyqtSignal(object)
134 134
135 135 # Emitted when an input request is received.
136 136 input_requested = QtCore.pyqtSignal(object)
137 137
138 138 #---------------------------------------------------------------------------
139 139 # 'RepSocketChannel' interface
140 140 #---------------------------------------------------------------------------
141 141
142 142 def call_handlers(self, msg):
143 143 """ Reimplemented to emit signals instead of making callbacks.
144 144 """
145 145 # Emit the generic signal.
146 146 self.message_received.emit(msg)
147 147
148 148 # Emit signals for specialized message types.
149 149 msg_type = msg['msg_type']
150 150 if msg_type == 'input_request':
151 151 self.input_requested.emit(msg)
152 152
153 153
154 154 class QtHBSocketChannel(SocketChannelQObject, HBSocketChannel):
155 155
156 156 # Emitted when the kernel has died.
157 157 kernel_died = QtCore.pyqtSignal(object)
158 158
159 159 #---------------------------------------------------------------------------
160 160 # 'HBSocketChannel' interface
161 161 #---------------------------------------------------------------------------
162 162
163 163 def call_handlers(self, since_last_heartbeat):
164 164 """ Reimplemented to emit signals instead of making callbacks.
165 165 """
166 166 # Emit the generic signal.
167 167 self.kernel_died.emit(since_last_heartbeat)
168 168
169 169
170 170 class QtKernelManager(KernelManager, SuperQObject):
171 171 """ A KernelManager that provides signals and slots.
172 172 """
173 173
174 174 __metaclass__ = MetaQObjectHasTraits
175 175
176 176 # Emitted when the kernel manager has started listening.
177 177 started_channels = QtCore.pyqtSignal()
178 178
179 179 # Emitted when the kernel manager has stopped listening.
180 180 stopped_channels = QtCore.pyqtSignal()
181 181
182 182 # Use Qt-specific channel classes that emit signals.
183 183 sub_channel_class = Type(QtSubSocketChannel)
184 184 xreq_channel_class = Type(QtXReqSocketChannel)
185 185 rep_channel_class = Type(QtRepSocketChannel)
186 186 hb_channel_class = Type(QtHBSocketChannel)
187 187
188 188 #---------------------------------------------------------------------------
189 189 # 'KernelManager' interface
190 190 #---------------------------------------------------------------------------
191 191
192 192 def start_channels(self, *args, **kw):
193 193 """ Reimplemented to emit signal.
194 194 """
195 195 super(QtKernelManager, self).start_channels(*args, **kw)
196 196 self.started_channels.emit()
197 197
198 198 def stop_channels(self):
199 199 """ Reimplemented to emit signal.
200 200 """
201 201 super(QtKernelManager, self).stop_channels()
202 202 self.stopped_channels.emit()
General Comments 0
You need to be logged in to leave comments. Login now