Show More
@@ -148,25 +148,12 b' class KernelClient(LoggingConfigurable, ConnectionFileMixin):' | |||
|
148 | 148 | return (self.shell_channel.is_alive() or self.iopub_channel.is_alive() or |
|
149 | 149 | self.stdin_channel.is_alive() or self.hb_channel.is_alive()) |
|
150 | 150 | |
|
151 | def _make_url(self, port): | |
|
152 | """Make a zmq url with a port. | |
|
153 | ||
|
154 | There are two cases that this handles: | |
|
155 | ||
|
156 | * tcp: tcp://ip:port | |
|
157 | * ipc: ipc://ip-port | |
|
158 | """ | |
|
159 | if self.transport == 'tcp': | |
|
160 | return "tcp://%s:%i" % (self.ip, port) | |
|
161 | else: | |
|
162 | return "%s://%s-%s" % (self.transport, self.ip, port) | |
|
163 | ||
|
164 | 151 | @property |
|
165 | 152 | def shell_channel(self): |
|
166 | 153 | """Get the shell channel object for this kernel.""" |
|
167 | 154 | if self._shell_channel is None: |
|
168 | 155 | self._shell_channel = self.shell_channel_class( |
|
169 |
self.context, self.session, self._make_url( |
|
|
156 | self.context, self.session, self._make_url('shell') | |
|
170 | 157 | ) |
|
171 | 158 | return self._shell_channel |
|
172 | 159 | |
@@ -175,7 +162,7 b' class KernelClient(LoggingConfigurable, ConnectionFileMixin):' | |||
|
175 | 162 | """Get the iopub channel object for this kernel.""" |
|
176 | 163 | if self._iopub_channel is None: |
|
177 | 164 | self._iopub_channel = self.iopub_channel_class( |
|
178 |
self.context, self.session, self._make_url( |
|
|
165 | self.context, self.session, self._make_url('iopub') | |
|
179 | 166 | ) |
|
180 | 167 | return self._iopub_channel |
|
181 | 168 | |
@@ -184,7 +171,7 b' class KernelClient(LoggingConfigurable, ConnectionFileMixin):' | |||
|
184 | 171 | """Get the stdin channel object for this kernel.""" |
|
185 | 172 | if self._stdin_channel is None: |
|
186 | 173 | self._stdin_channel = self.stdin_channel_class( |
|
187 |
self.context, self.session, self._make_url( |
|
|
174 | self.context, self.session, self._make_url('stdin') | |
|
188 | 175 | ) |
|
189 | 176 | return self._stdin_channel |
|
190 | 177 | |
@@ -193,7 +180,7 b' class KernelClient(LoggingConfigurable, ConnectionFileMixin):' | |||
|
193 | 180 | """Get the hb channel object for this kernel.""" |
|
194 | 181 | if self._hb_channel is None: |
|
195 | 182 | self._hb_channel = self.hb_channel_class( |
|
196 |
self.context, self.session, self._make_url( |
|
|
183 | self.context, self.session, self._make_url('hb') | |
|
197 | 184 | ) |
|
198 | 185 | return self._hb_channel |
|
199 | 186 |
@@ -17,6 +17,8 b' Authors:' | |||
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | from __future__ import absolute_import | |
|
21 | ||
|
20 | 22 | import glob |
|
21 | 23 | import json |
|
22 | 24 | import os |
@@ -26,6 +28,8 b' from getpass import getpass' | |||
|
26 | 28 | from subprocess import Popen, PIPE |
|
27 | 29 | import tempfile |
|
28 | 30 | |
|
31 | import zmq | |
|
32 | ||
|
29 | 33 | # external imports |
|
30 | 34 | from IPython.external.ssh import tunnel |
|
31 | 35 | |
@@ -353,8 +357,17 b' def tunnel_to_kernel(connection_info, sshserver, sshkey=None):' | |||
|
353 | 357 | |
|
354 | 358 | |
|
355 | 359 | #----------------------------------------------------------------------------- |
|
356 |
# Mixin for classes that work |
|
|
360 | # Mixin for classes that work with connection files | |
|
357 | 361 | #----------------------------------------------------------------------------- |
|
362 | ||
|
363 | channel_socket_types = { | |
|
364 | 'hb' : zmq.REQ, | |
|
365 | 'shell' : zmq.DEALER, | |
|
366 | 'iopub' : zmq.SUB, | |
|
367 | 'stdin' : zmq.DEALER, | |
|
368 | 'control': zmq.DEALER, | |
|
369 | } | |
|
370 | ||
|
358 | 371 | port_names = [ "%s_port" % channel for channel in ('shell', 'stdin', 'iopub', 'hb', 'control')] |
|
359 | 372 | |
|
360 | 373 | class ConnectionFileMixin(HasTraits): |
@@ -466,6 +479,53 b' class ConnectionFileMixin(HasTraits):' | |||
|
466 | 479 | setattr(self, name, cfg[name]) |
|
467 | 480 | self.session.key = str_to_bytes(cfg['key']) |
|
468 | 481 | |
|
482 | #-------------------------------------------------------------------------- | |
|
483 | # Creating connected sockets | |
|
484 | #-------------------------------------------------------------------------- | |
|
485 | ||
|
486 | def _make_url(self, channel): | |
|
487 | """Make a ZeroMQ URL for a given channel.""" | |
|
488 | transport = self.transport | |
|
489 | ip = self.ip | |
|
490 | port = getattr(self, '%s_port' % channel) | |
|
491 | ||
|
492 | if transport == 'tcp': | |
|
493 | return "tcp://%s:%i" % (ip, port) | |
|
494 | else: | |
|
495 | return "%s://%s-%s" % (transport, ip, port) | |
|
496 | ||
|
497 | def _create_connected_socket(self, channel, identity=None): | |
|
498 | """Create a zmq Socket and connect it to the kernel.""" | |
|
499 | url = self._make_url(channel) | |
|
500 | socket_type = channel_socket_types[channel] | |
|
501 | self.log.info("Connecting to: %s" % url) | |
|
502 | sock = self.context.socket(socket_type) | |
|
503 | if identity: | |
|
504 | sock.identity = identity | |
|
505 | sock.connect(url) | |
|
506 | return sock | |
|
507 | ||
|
508 | def connect_iopub(self, identity=None): | |
|
509 | """return zmq Socket connected to the IOPub channel""" | |
|
510 | sock = self._create_connected_socket('iopub', identity=identity) | |
|
511 | sock.setsockopt(zmq.SUBSCRIBE, b'') | |
|
512 | return sock | |
|
513 | ||
|
514 | def connect_shell(self, identity=None): | |
|
515 | """return zmq Socket connected to the Shell channel""" | |
|
516 | return self._create_connected_socket('shell', identity=identity) | |
|
517 | ||
|
518 | def connect_stdin(self, identity=None): | |
|
519 | """return zmq Socket connected to the StdIn channel""" | |
|
520 | return self._create_connected_socket('stdin', identity=identity) | |
|
521 | ||
|
522 | def connect_hb(self, identity=None): | |
|
523 | """return zmq Socket connected to the Heartbeat channel""" | |
|
524 | return self._create_connected_socket('hb', identity=identity) | |
|
525 | ||
|
526 | def connect_control(self, identity=None): | |
|
527 | """return zmq Socket connected to the Heartbeat channel""" | |
|
528 | return self._create_connected_socket('control', identity=identity) | |
|
469 | 529 | |
|
470 | 530 | |
|
471 | 531 | __all__ = [ |
@@ -42,14 +42,6 b' from .managerabc import (' | |||
|
42 | 42 | # Main kernel manager class |
|
43 | 43 | #----------------------------------------------------------------------------- |
|
44 | 44 | |
|
45 | _socket_types = { | |
|
46 | 'hb' : zmq.REQ, | |
|
47 | 'shell' : zmq.DEALER, | |
|
48 | 'iopub' : zmq.SUB, | |
|
49 | 'stdin' : zmq.DEALER, | |
|
50 | 'control': zmq.DEALER, | |
|
51 | } | |
|
52 | ||
|
53 | 45 | class KernelManager(LoggingConfigurable, ConnectionFileMixin): |
|
54 | 46 | """Manages a single kernel in a subprocess on this host. |
|
55 | 47 | |
@@ -145,54 +137,6 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):' | |||
|
145 | 137 | return self.client_factory(**kw) |
|
146 | 138 | |
|
147 | 139 | #-------------------------------------------------------------------------- |
|
148 | # Connection info | |
|
149 | #-------------------------------------------------------------------------- | |
|
150 | ||
|
151 | def _make_url(self, channel): | |
|
152 | """Make a ZeroMQ URL for a given channel.""" | |
|
153 | transport = self.transport | |
|
154 | ip = self.ip | |
|
155 | port = getattr(self, '%s_port' % channel) | |
|
156 | ||
|
157 | if transport == 'tcp': | |
|
158 | return "tcp://%s:%i" % (ip, port) | |
|
159 | else: | |
|
160 | return "%s://%s-%s" % (transport, ip, port) | |
|
161 | ||
|
162 | def _create_connected_socket(self, channel, identity=None): | |
|
163 | """Create a zmq Socket and connect it to the kernel.""" | |
|
164 | url = self._make_url(channel) | |
|
165 | socket_type = _socket_types[channel] | |
|
166 | self.log.info("Connecting to: %s" % url) | |
|
167 | sock = self.context.socket(socket_type) | |
|
168 | if identity: | |
|
169 | sock.identity = identity | |
|
170 | sock.connect(url) | |
|
171 | return sock | |
|
172 | ||
|
173 | def connect_iopub(self, identity=None): | |
|
174 | """return zmq Socket connected to the IOPub channel""" | |
|
175 | sock = self._create_connected_socket('iopub', identity=identity) | |
|
176 | sock.setsockopt(zmq.SUBSCRIBE, b'') | |
|
177 | return sock | |
|
178 | ||
|
179 | def connect_shell(self, identity=None): | |
|
180 | """return zmq Socket connected to the Shell channel""" | |
|
181 | return self._create_connected_socket('shell', identity=identity) | |
|
182 | ||
|
183 | def connect_stdin(self, identity=None): | |
|
184 | """return zmq Socket connected to the StdIn channel""" | |
|
185 | return self._create_connected_socket('stdin', identity=identity) | |
|
186 | ||
|
187 | def connect_hb(self, identity=None): | |
|
188 | """return zmq Socket connected to the Heartbeat channel""" | |
|
189 | return self._create_connected_socket('hb', identity=identity) | |
|
190 | ||
|
191 | def connect_control(self, identity=None): | |
|
192 | """return zmq Socket connected to the Heartbeat channel""" | |
|
193 | return self._create_connected_socket('control', identity=identity) | |
|
194 | ||
|
195 | #-------------------------------------------------------------------------- | |
|
196 | 140 | # Kernel management |
|
197 | 141 | #-------------------------------------------------------------------------- |
|
198 | 142 |
General Comments 0
You need to be logged in to leave comments.
Login now