Show More
@@ -1,20 +1,7 b'' | |||
|
1 | """Tornado handlers for WebSocket <-> ZMQ sockets. | |
|
1 | """Tornado handlers for WebSocket <-> ZMQ sockets.""" | |
|
2 | 2 | |
|
3 | Authors: | |
|
4 | ||
|
5 | * Brian Granger | |
|
6 | """ | |
|
7 | ||
|
8 | #----------------------------------------------------------------------------- | |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
10 | # | |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
|
12 | # the file COPYING, distributed as part of this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) IPython Development Team. | |
|
4 | # Distributed under the terms of the Modified BSD License. | |
|
18 | 5 | |
|
19 | 6 | try: |
|
20 | 7 | from urllib.parse import urlparse # Py 3 |
@@ -37,9 +24,6 b' from IPython.utils.py3compat import PY3, cast_unicode' | |||
|
37 | 24 | |
|
38 | 25 | from .handlers import IPythonHandler |
|
39 | 26 | |
|
40 | #----------------------------------------------------------------------------- | |
|
41 | # ZMQ handlers | |
|
42 | #----------------------------------------------------------------------------- | |
|
43 | 27 | |
|
44 | 28 | class ZMQStreamHandler(websocket.WebSocketHandler): |
|
45 | 29 |
@@ -1,20 +1,7 b'' | |||
|
1 |
"""Tornado handlers for |
|
|
1 | """Tornado handlers for kernels.""" | |
|
2 | 2 | |
|
3 | Authors: | |
|
4 | ||
|
5 | * Brian Granger | |
|
6 | """ | |
|
7 | ||
|
8 | #----------------------------------------------------------------------------- | |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
10 | # | |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
|
12 | # the file COPYING, distributed as part of this software. | |
|
13 | #----------------------------------------------------------------------------- | |
|
14 | ||
|
15 | #----------------------------------------------------------------------------- | |
|
16 | # Imports | |
|
17 | #----------------------------------------------------------------------------- | |
|
3 | # Copyright (c) IPython Development Team. | |
|
4 | # Distributed under the terms of the Modified BSD License. | |
|
18 | 5 | |
|
19 | 6 | import logging |
|
20 | 7 | from tornado import web |
@@ -22,15 +9,13 b' from tornado import web' | |||
|
22 | 9 | from zmq.utils import jsonapi |
|
23 | 10 | |
|
24 | 11 | from IPython.utils.jsonutil import date_default |
|
12 | from IPython.utils.py3compat import string_types | |
|
25 | 13 | from IPython.html.utils import url_path_join, url_escape |
|
26 | 14 | |
|
27 | 15 | from ...base.handlers import IPythonHandler, json_errors |
|
28 | 16 | from ...base.zmqhandlers import AuthenticatedZMQStreamHandler |
|
29 | 17 | |
|
30 | #----------------------------------------------------------------------------- | |
|
31 | # Kernel handlers | |
|
32 | #----------------------------------------------------------------------------- | |
|
33 | ||
|
18 | from IPython.core.release import kernel_protocol_version | |
|
34 | 19 | |
|
35 | 20 | class MainKernelHandler(IPythonHandler): |
|
36 | 21 | |
@@ -96,6 +81,34 b' class ZMQChannelHandler(AuthenticatedZMQStreamHandler):' | |||
|
96 | 81 | km = self.kernel_manager |
|
97 | 82 | meth = getattr(km, 'connect_%s' % self.channel) |
|
98 | 83 | self.zmq_stream = meth(self.kernel_id, identity=self.session.bsession) |
|
84 | self.kernel_info_channel = None | |
|
85 | self.kernel_info_channel = km.connect_shell(self.kernel_id) | |
|
86 | self.kernel_info_channel.on_recv(self._handle_kernel_info) | |
|
87 | self._request_kernel_info() | |
|
88 | ||
|
89 | def _request_kernel_info(self): | |
|
90 | self.log.debug("requesting kernel info") | |
|
91 | self.session.send(self.kernel_info_channel, "kernel_info_request") | |
|
92 | ||
|
93 | def _handle_kernel_info(self, msg): | |
|
94 | idents,msg = self.session.feed_identities(msg) | |
|
95 | try: | |
|
96 | msg = self.session.unserialize(msg) | |
|
97 | except: | |
|
98 | self.log.error("Bad kernel_info reply", exc_info=True) | |
|
99 | self._request_kernel_info() | |
|
100 | return | |
|
101 | else: | |
|
102 | if msg['msg_type'] != 'kernel_info_reply' or 'protocol_version' not in msg['content']: | |
|
103 | self.log.error("Kernel info request failed, assuming current %s", msg['content']) | |
|
104 | else: | |
|
105 | protocol_version = msg['content']['protocol_version'] | |
|
106 | if protocol_version != kernel_protocol_version: | |
|
107 | self.session.adapt_version = int(protocol_version.split('.')[0]) | |
|
108 | self.log.info("adapting kernel to %s" % protocol_version) | |
|
109 | self.kernel_info_channel.close() | |
|
110 | self.kernel_info_channel = None | |
|
111 | ||
|
99 | 112 | |
|
100 | 113 | def initialize(self, *args, **kwargs): |
|
101 | 114 | self.zmq_stream = None |
@@ -63,7 +63,10 b' class BlockingIOPubChannel(BlockingChannelMixin, IOPubChannel):' | |||
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | class BlockingShellChannel(BlockingChannelMixin, ShellChannel): |
|
66 | pass | |
|
66 | def call_handlers(self, msg): | |
|
67 | if msg['msg_type'] == 'kernel_info_reply': | |
|
68 | self._handle_kernel_info_reply(msg) | |
|
69 | return super(BlockingShellChannel, self).call_handlers(msg) | |
|
67 | 70 | |
|
68 | 71 | |
|
69 | 72 | class BlockingStdInChannel(BlockingChannelMixin, StdInChannel): |
@@ -16,7 +16,8 b' import zmq' | |||
|
16 | 16 | from zmq import ZMQError |
|
17 | 17 | from zmq.eventloop import ioloop, zmqstream |
|
18 | 18 | |
|
19 | # Local imports | |
|
19 | from IPython.core.release import kernel_protocol_version_info | |
|
20 | ||
|
20 | 21 | from .channelsabc import ( |
|
21 | 22 | ShellChannelABC, IOPubChannelABC, |
|
22 | 23 | HBChannelABC, StdInChannelABC, |
@@ -27,6 +28,8 b' from IPython.utils.py3compat import string_types, iteritems' | |||
|
27 | 28 | # Constants and exceptions |
|
28 | 29 | #----------------------------------------------------------------------------- |
|
29 | 30 | |
|
31 | major_protocol_version = kernel_protocol_version_info[0] | |
|
32 | ||
|
30 | 33 | class InvalidPortNumber(Exception): |
|
31 | 34 | pass |
|
32 | 35 | |
@@ -173,7 +176,8 b' class ZMQSocketChannel(Thread):' | |||
|
173 | 176 | Unpacks message, and calls handlers with it. |
|
174 | 177 | """ |
|
175 | 178 | ident,smsg = self.session.feed_identities(msg) |
|
176 |
|
|
|
179 | msg = self.session.unserialize(smsg) | |
|
180 | self.call_handlers(msg) | |
|
177 | 181 | |
|
178 | 182 | |
|
179 | 183 | |
@@ -195,7 +199,7 b' class ShellChannel(ZMQSocketChannel):' | |||
|
195 | 199 | def __init__(self, context, session, address): |
|
196 | 200 | super(ShellChannel, self).__init__(context, session, address) |
|
197 | 201 | self.ioloop = ioloop.IOLoop() |
|
198 | ||
|
202 | ||
|
199 | 203 | def run(self): |
|
200 | 204 | """The thread's main activity. Call start() instead.""" |
|
201 | 205 | self.socket = self.context.socket(zmq.DEALER) |
@@ -365,6 +369,15 b' class ShellChannel(ZMQSocketChannel):' | |||
|
365 | 369 | msg = self.session.msg('kernel_info_request') |
|
366 | 370 | self._queue_send(msg) |
|
367 | 371 | return msg['header']['msg_id'] |
|
372 | ||
|
373 | def _handle_kernel_info_reply(self, msg): | |
|
374 | """handle kernel info reply | |
|
375 | ||
|
376 | sets protocol adaptation version | |
|
377 | """ | |
|
378 | adapt_version = int(msg['content']['protocol_version'].split('.')[0]) | |
|
379 | if adapt_version != major_protocol_version: | |
|
380 | self.session.adapt_version = adapt_version | |
|
368 | 381 | |
|
369 | 382 | def shutdown(self, restart=False): |
|
370 | 383 | """Request an immediate kernel shutdown. |
@@ -59,6 +59,7 b' class QtShellChannelMixin(ChannelQObject):' | |||
|
59 | 59 | complete_reply = QtCore.Signal(object) |
|
60 | 60 | inspect_reply = QtCore.Signal(object) |
|
61 | 61 | history_reply = QtCore.Signal(object) |
|
62 | kernel_info_reply = QtCore.Signal(object) | |
|
62 | 63 | |
|
63 | 64 | #--------------------------------------------------------------------------- |
|
64 | 65 | # 'ShellChannel' interface |
@@ -72,6 +73,9 b' class QtShellChannelMixin(ChannelQObject):' | |||
|
72 | 73 | |
|
73 | 74 | # Emit signals for specialized message types. |
|
74 | 75 | msg_type = msg['header']['msg_type'] |
|
76 | if msg_type == 'kernel_info_reply': | |
|
77 | self._handle_kernel_info_reply(msg) | |
|
78 | ||
|
75 | 79 | signal = getattr(self, msg_type, None) |
|
76 | 80 | if signal: |
|
77 | 81 | signal.emit(msg) |
General Comments 0
You need to be logged in to leave comments.
Login now