##// END OF EJS Templates
Move kernel_info for adaptation onto KernelClient
Thomas Kluyver -
Show More
@@ -143,21 +143,6 b' class BlockingShellChannel(ZMQSocketChannel):'
143 def start(self):
143 def start(self):
144 self.socket = make_stdin_socket(self.context, self.session.bsession, self.address)
144 self.socket = make_stdin_socket(self.context, self.session.bsession, self.address)
145
145
146 def _handle_kernel_info_reply(self, msg):
147 """handle kernel info reply
148
149 sets protocol adaptation version
150 """
151 adapt_version = int(msg['content']['protocol_version'].split('.')[0])
152 if adapt_version != major_protocol_version:
153 self.session.adapt_version = adapt_version
154
155 def _recv(self, **kwargs):
156 # Listen for kernel_info_reply message to do protocol adaptation
157 msg = ZMQSocketChannel._recv(self, **kwargs)
158 if msg['msg_type'] == 'kernel_info_reply':
159 self._handle_kernel_info_reply(msg)
160 return msg
161
146
162 class BlockingIOPubChannel(ZMQSocketChannel):
147 class BlockingIOPubChannel(ZMQSocketChannel):
163 """The iopub channel which listens for messages that the kernel publishes.
148 """The iopub channel which listens for messages that the kernel publishes.
@@ -169,9 +154,6 b' class BlockingIOPubChannel(ZMQSocketChannel):'
169
154
170 class BlockingStdInChannel(ZMQSocketChannel):
155 class BlockingStdInChannel(ZMQSocketChannel):
171 """The stdin channel to handle raw_input requests that the kernel makes."""
156 """The stdin channel to handle raw_input requests that the kernel makes."""
172 msg_queue = None
173 proxy_methods = ['input']
174
175 def start(self):
157 def start(self):
176 self.socket = make_stdin_socket(self.context, self.session.bsession, self.address)
158 self.socket = make_stdin_socket(self.context, self.session.bsession, self.address)
177
159
@@ -2,16 +2,13 b''
2
2
3 Useful for test suites and blocking terminal interfaces.
3 Useful for test suites and blocking terminal interfaces.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 # Copyright (c) IPython Development Team.
6 # Copyright (C) 2013 The IPython Development Team
6 # Distributed under the terms of the Modified BSD License.
7 #
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING.txt, distributed as part of this software.
10 #-----------------------------------------------------------------------------
11
7
12 #-----------------------------------------------------------------------------
8 try:
13 # Imports
9 from queue import Empty # Python 3
14 #-----------------------------------------------------------------------------
10 except ImportError:
11 from Queue import Empty # Python 2
15
12
16 from IPython.utils.traitlets import Type
13 from IPython.utils.traitlets import Type
17 from IPython.kernel.client import KernelClient
14 from IPython.kernel.client import KernelClient
@@ -20,11 +17,22 b' from .channels import ('
20 BlockingShellChannel, BlockingStdInChannel
17 BlockingShellChannel, BlockingStdInChannel
21 )
18 )
22
19
23 #-----------------------------------------------------------------------------
24 # Blocking kernel manager
25 #-----------------------------------------------------------------------------
26
27 class BlockingKernelClient(KernelClient):
20 class BlockingKernelClient(KernelClient):
21 def wait_for_ready(self):
22 # Wait for kernel info reply on shell channel
23 while True:
24 msg = self.shell_channel.get_msg(block=True)
25 if msg['msg_type'] == 'kernel_info_reply':
26 self._handle_kernel_info_reply(msg)
27 break
28
29 # Flush IOPub channel
30 while True:
31 try:
32 msg = self.iopub_channel.get_msg(block=True, timeout=0.2)
33 print(msg['msg_type'])
34 except Empty:
35 break
28
36
29 # The classes to use for the various channels
37 # The classes to use for the various channels
30 shell_channel_class = Type(BlockingShellChannel)
38 shell_channel_class = Type(BlockingShellChannel)
@@ -4,7 +4,7 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from __future__ import absolute_import
6 from __future__ import absolute_import
7 from IPython.kernel.channels import validate_string_dict
7 from IPython.kernel.channels import validate_string_dict, major_protocol_version
8 from IPython.utils.py3compat import string_types
8 from IPython.utils.py3compat import string_types
9
9
10 import zmq
10 import zmq
@@ -90,6 +90,7 b' class KernelClient(ConnectionFileMixin):'
90 """
90 """
91 if shell:
91 if shell:
92 self.shell_channel.start()
92 self.shell_channel.start()
93 self.kernel_info()
93 if iopub:
94 if iopub:
94 self.iopub_channel.start()
95 self.iopub_channel.start()
95 if stdin:
96 if stdin:
@@ -327,6 +328,15 b' class KernelClient(ConnectionFileMixin):'
327 self.shell_channel._queue_send(msg)
328 self.shell_channel._queue_send(msg)
328 return msg['header']['msg_id']
329 return msg['header']['msg_id']
329
330
331 def _handle_kernel_info_reply(self, msg):
332 """handle kernel info reply
333
334 sets protocol adaptation version
335 """
336 adapt_version = int(msg['content']['protocol_version'].split('.')[0])
337 if adapt_version != major_protocol_version:
338 self.session.adapt_version = adapt_version
339
330 def shutdown(self, restart=False):
340 def shutdown(self, restart=False):
331 """Request an immediate kernel shutdown.
341 """Request an immediate kernel shutdown.
332
342
@@ -421,17 +421,8 b" def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs):"
421 km.start_kernel(**kwargs)
421 km.start_kernel(**kwargs)
422 kc = km.client()
422 kc = km.client()
423 kc.start_channels()
423 kc.start_channels()
424 kc.wait_for_ready()
424
425
425 kc.kernel_info()
426 kc.get_shell_msg(block=True, timeout=startup_timeout)
427
428 # Flush channels
429 for channel in (kc.shell_channel, kc.iopub_channel):
430 while True:
431 try:
432 channel.get_msg(block=True, timeout=0.1)
433 except Empty:
434 break
435 return km, kc
426 return km, kc
436
427
437 @contextmanager
428 @contextmanager
@@ -92,6 +92,7 b' def setup_kernel(cmd):'
92 client = BlockingKernelClient(connection_file=connection_file)
92 client = BlockingKernelClient(connection_file=connection_file)
93 client.load_connection_file()
93 client.load_connection_file()
94 client.start_channels()
94 client.start_channels()
95 client.wait_for_ready()
95
96
96 try:
97 try:
97 yield client
98 yield client
@@ -366,6 +366,14 b' StdInChannelABC.register(QtStdInChannel)'
366 class QtKernelClient(QtKernelClientMixin, KernelClient):
366 class QtKernelClient(QtKernelClientMixin, KernelClient):
367 """ A KernelClient that provides signals and slots.
367 """ A KernelClient that provides signals and slots.
368 """
368 """
369 def start_channels(self, shell=True, iopub=True, stdin=True, hb=True):
370 if shell:
371 self.shell_channel.kernel_info_reply.connect(self._handle_kernel_info_reply)
372 super(QtKernelClient, self).start_channels(shell, iopub, stdin, hb)
373
374 def _handle_kernel_info_reply(self, msg):
375 super(QtKernelClient, self)._handle_kernel_info_reply(msg)
376 self.shell_channel.kernel_info_reply.disconnect(self._handle_kernel_info_reply)
369
377
370 iopub_channel_class = Type(QtIOPubChannel)
378 iopub_channel_class = Type(QtIOPubChannel)
371 shell_channel_class = Type(QtShellChannel)
379 shell_channel_class = Type(QtShellChannel)
General Comments 0
You need to be logged in to leave comments. Login now