##// END OF EJS Templates
add QtKernelManager with restarter
MinRK -
Show More
@@ -0,0 +1,52 b''
1 """ Defines a KernelClient that provides signals and slots.
2 """
3
4 from IPython.external.qt import QtCore
5
6 # Local imports
7 from IPython.utils.traitlets import Bool, Instance
8
9 from IPython.kernel import KernelManager
10 from IPython.kernel.restarter import KernelRestarter
11
12 from .kernel_mixins import QtKernelManagerMixin, QtKernelRestarterMixin
13
14
15 class QtKernelRestarter(KernelRestarter, QtKernelRestarterMixin):
16
17 def start(self):
18 if self._timer is None:
19 self._timer = QtCore.QTimer()
20 self._timer.timeout.connect(self.poll)
21 self._timer.start(self.time_to_dead * 1000)
22
23 def stop(self):
24 self._timer.stop()
25
26 def poll(self):
27 super(QtKernelRestarter, self).poll()
28
29
30 class QtKernelManager(KernelManager, QtKernelManagerMixin):
31 """A KernelManager with Qt signals for restart"""
32
33 autorestart = Bool(True, config=True)
34
35 def start_restarter(self):
36 if self.autorestart and self.has_kernel:
37 if self._restarter is None:
38 self._restarter = QtKernelRestarter(
39 kernel_manager=self,
40 config=self.config,
41 log=self.log,
42 )
43 self._restarter.register_callback(self._handle_kernel_restarted)
44 self._restarter.start()
45
46 def stop_restarter(self):
47 if self.autorestart:
48 if self._restarter is not None:
49 self._restarter.stop()
50
51 def _handle_kernel_restarted(self):
52 self.kernel_restarted.emit()
@@ -1,205 +1,220 b''
1 1 """ Defines a KernelManager that provides signals and slots.
2 2 """
3 3
4 4 # System library imports.
5 5 from IPython.external.qt import QtCore
6 6
7 7 # IPython imports.
8 8 from IPython.utils.traitlets import HasTraits, Type
9 9 from util import MetaQObjectHasTraits, SuperQObject
10 10
11 11
12 12 class ChannelQObject(SuperQObject):
13 13
14 14 # Emitted when the channel is started.
15 15 started = QtCore.Signal()
16 16
17 17 # Emitted when the channel is stopped.
18 18 stopped = QtCore.Signal()
19 19
20 20 #---------------------------------------------------------------------------
21 21 # Channel interface
22 22 #---------------------------------------------------------------------------
23 23
24 24 def start(self):
25 25 """ Reimplemented to emit signal.
26 26 """
27 27 super(ChannelQObject, self).start()
28 28 self.started.emit()
29 29
30 30 def stop(self):
31 31 """ Reimplemented to emit signal.
32 32 """
33 33 super(ChannelQObject, self).stop()
34 34 self.stopped.emit()
35 35
36 36 #---------------------------------------------------------------------------
37 37 # InProcessChannel interface
38 38 #---------------------------------------------------------------------------
39 39
40 40 def call_handlers_later(self, *args, **kwds):
41 41 """ Call the message handlers later.
42 42 """
43 43 do_later = lambda: self.call_handlers(*args, **kwds)
44 44 QtCore.QTimer.singleShot(0, do_later)
45 45
46 46 def process_events(self):
47 47 """ Process any pending GUI events.
48 48 """
49 49 QtCore.QCoreApplication.instance().processEvents()
50 50
51 51
52 52 class QtShellChannelMixin(ChannelQObject):
53 53
54 54 # Emitted when any message is received.
55 55 message_received = QtCore.Signal(object)
56 56
57 57 # Emitted when a reply has been received for the corresponding request type.
58 58 execute_reply = QtCore.Signal(object)
59 59 complete_reply = QtCore.Signal(object)
60 60 object_info_reply = QtCore.Signal(object)
61 61 history_reply = QtCore.Signal(object)
62 62
63 63 #---------------------------------------------------------------------------
64 64 # 'ShellChannel' interface
65 65 #---------------------------------------------------------------------------
66 66
67 67 def call_handlers(self, msg):
68 68 """ Reimplemented to emit signals instead of making callbacks.
69 69 """
70 70 # Emit the generic signal.
71 71 self.message_received.emit(msg)
72 72
73 73 # Emit signals for specialized message types.
74 74 msg_type = msg['header']['msg_type']
75 75 signal = getattr(self, msg_type, None)
76 76 if signal:
77 77 signal.emit(msg)
78 78
79 79
80 80 class QtIOPubChannelMixin(ChannelQObject):
81 81
82 82 # Emitted when any message is received.
83 83 message_received = QtCore.Signal(object)
84 84
85 85 # Emitted when a message of type 'stream' is received.
86 86 stream_received = QtCore.Signal(object)
87 87
88 88 # Emitted when a message of type 'pyin' is received.
89 89 pyin_received = QtCore.Signal(object)
90 90
91 91 # Emitted when a message of type 'pyout' is received.
92 92 pyout_received = QtCore.Signal(object)
93 93
94 94 # Emitted when a message of type 'pyerr' is received.
95 95 pyerr_received = QtCore.Signal(object)
96 96
97 97 # Emitted when a message of type 'display_data' is received
98 98 display_data_received = QtCore.Signal(object)
99 99
100 100 # Emitted when a crash report message is received from the kernel's
101 101 # last-resort sys.excepthook.
102 102 crash_received = QtCore.Signal(object)
103 103
104 104 # Emitted when a shutdown is noticed.
105 105 shutdown_reply_received = QtCore.Signal(object)
106 106
107 107 #---------------------------------------------------------------------------
108 108 # 'IOPubChannel' interface
109 109 #---------------------------------------------------------------------------
110 110
111 111 def call_handlers(self, msg):
112 112 """ Reimplemented to emit signals instead of making callbacks.
113 113 """
114 114 # Emit the generic signal.
115 115 self.message_received.emit(msg)
116 116 # Emit signals for specialized message types.
117 117 msg_type = msg['header']['msg_type']
118 118 signal = getattr(self, msg_type + '_received', None)
119 119 if signal:
120 120 signal.emit(msg)
121 121 elif msg_type in ('stdout', 'stderr'):
122 122 self.stream_received.emit(msg)
123 123
124 124 def flush(self):
125 125 """ Reimplemented to ensure that signals are dispatched immediately.
126 126 """
127 127 super(QtIOPubChannelMixin, self).flush()
128 128 QtCore.QCoreApplication.instance().processEvents()
129 129
130 130
131 131 class QtStdInChannelMixin(ChannelQObject):
132 132
133 133 # Emitted when any message is received.
134 134 message_received = QtCore.Signal(object)
135 135
136 136 # Emitted when an input request is received.
137 137 input_requested = QtCore.Signal(object)
138 138
139 139 #---------------------------------------------------------------------------
140 140 # 'StdInChannel' interface
141 141 #---------------------------------------------------------------------------
142 142
143 143 def call_handlers(self, msg):
144 144 """ Reimplemented to emit signals instead of making callbacks.
145 145 """
146 146 # Emit the generic signal.
147 147 self.message_received.emit(msg)
148 148
149 149 # Emit signals for specialized message types.
150 150 msg_type = msg['header']['msg_type']
151 151 if msg_type == 'input_request':
152 152 self.input_requested.emit(msg)
153 153
154 154
155 155 class QtHBChannelMixin(ChannelQObject):
156 156
157 157 # Emitted when the kernel has died.
158 158 kernel_died = QtCore.Signal(object)
159 159
160 160 #---------------------------------------------------------------------------
161 161 # 'HBChannel' interface
162 162 #---------------------------------------------------------------------------
163 163
164 164 def call_handlers(self, since_last_heartbeat):
165 165 """ Reimplemented to emit signals instead of making callbacks.
166 166 """
167 167 # Emit the generic signal.
168 168 self.kernel_died.emit(since_last_heartbeat)
169 169
170 170
171 class QtKernelRestarterMixin(HasTraits, SuperQObject):
172
173 __metaclass__ = MetaQObjectHasTraits
174 _timer = None
175
176
177 class QtKernelManagerMixin(HasTraits, SuperQObject):
178 """ A KernelClient that provides signals and slots.
179 """
180
181 __metaclass__ = MetaQObjectHasTraits
182
183 kernel_restarted = QtCore.Signal()
184
185
171 186 class QtKernelClientMixin(HasTraits, SuperQObject):
172 187 """ A KernelClient that provides signals and slots.
173 188 """
174 189
175 190 __metaclass__ = MetaQObjectHasTraits
176 191
177 192 # Emitted when the kernel client has started listening.
178 193 started_channels = QtCore.Signal()
179 194
180 195 # Emitted when the kernel client has stopped listening.
181 196 stopped_channels = QtCore.Signal()
182 197
183 198 # Use Qt-specific channel classes that emit signals.
184 199 iopub_channel_class = Type(QtIOPubChannelMixin)
185 200 shell_channel_class = Type(QtShellChannelMixin)
186 201 stdin_channel_class = Type(QtStdInChannelMixin)
187 202 hb_channel_class = Type(QtHBChannelMixin)
188 203
189 204 #---------------------------------------------------------------------------
190 205 # 'KernelClient' interface
191 206 #---------------------------------------------------------------------------
192 207
193 208 #------ Channel management -------------------------------------------------
194 209
195 210 def start_channels(self, *args, **kw):
196 211 """ Reimplemented to emit signal.
197 212 """
198 213 super(QtKernelClientMixin, self).start_channels(*args, **kw)
199 214 self.started_channels.emit()
200 215
201 216 def stop_channels(self):
202 217 """ Reimplemented to emit signal.
203 218 """
204 219 super(QtKernelClientMixin, self).stop_channels()
205 220 self.stopped_channels.emit()
General Comments 0
You need to be logged in to leave comments. Login now