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