diff --git a/IPython/kernel/client.py b/IPython/kernel/client.py index ead8e44..8ca6086 100644 --- a/IPython/kernel/client.py +++ b/IPython/kernel/client.py @@ -148,25 +148,12 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): return (self.shell_channel.is_alive() or self.iopub_channel.is_alive() or self.stdin_channel.is_alive() or self.hb_channel.is_alive()) - def _make_url(self, port): - """Make a zmq url with a port. - - There are two cases that this handles: - - * tcp: tcp://ip:port - * ipc: ipc://ip-port - """ - if self.transport == 'tcp': - return "tcp://%s:%i" % (self.ip, port) - else: - return "%s://%s-%s" % (self.transport, self.ip, port) - @property def shell_channel(self): """Get the shell channel object for this kernel.""" if self._shell_channel is None: self._shell_channel = self.shell_channel_class( - self.context, self.session, self._make_url(self.shell_port) + self.context, self.session, self._make_url('shell') ) return self._shell_channel @@ -175,7 +162,7 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): """Get the iopub channel object for this kernel.""" if self._iopub_channel is None: self._iopub_channel = self.iopub_channel_class( - self.context, self.session, self._make_url(self.iopub_port) + self.context, self.session, self._make_url('iopub') ) return self._iopub_channel @@ -184,7 +171,7 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): """Get the stdin channel object for this kernel.""" if self._stdin_channel is None: self._stdin_channel = self.stdin_channel_class( - self.context, self.session, self._make_url(self.stdin_port) + self.context, self.session, self._make_url('stdin') ) return self._stdin_channel @@ -193,7 +180,7 @@ class KernelClient(LoggingConfigurable, ConnectionFileMixin): """Get the hb channel object for this kernel.""" if self._hb_channel is None: self._hb_channel = self.hb_channel_class( - self.context, self.session, self._make_url(self.hb_port) + self.context, self.session, self._make_url('hb') ) return self._hb_channel diff --git a/IPython/kernel/connect.py b/IPython/kernel/connect.py index 329c7b9..c6bab66 100644 --- a/IPython/kernel/connect.py +++ b/IPython/kernel/connect.py @@ -17,6 +17,8 @@ Authors: # Imports #----------------------------------------------------------------------------- +from __future__ import absolute_import + import glob import json import os @@ -26,6 +28,8 @@ from getpass import getpass from subprocess import Popen, PIPE import tempfile +import zmq + # external imports from IPython.external.ssh import tunnel @@ -353,8 +357,17 @@ def tunnel_to_kernel(connection_info, sshserver, sshkey=None): #----------------------------------------------------------------------------- -# Mixin for classes that workw ith connection files +# Mixin for classes that work with connection files #----------------------------------------------------------------------------- + +channel_socket_types = { + 'hb' : zmq.REQ, + 'shell' : zmq.DEALER, + 'iopub' : zmq.SUB, + 'stdin' : zmq.DEALER, + 'control': zmq.DEALER, +} + port_names = [ "%s_port" % channel for channel in ('shell', 'stdin', 'iopub', 'hb', 'control')] class ConnectionFileMixin(HasTraits): @@ -466,6 +479,53 @@ class ConnectionFileMixin(HasTraits): setattr(self, name, cfg[name]) self.session.key = str_to_bytes(cfg['key']) + #-------------------------------------------------------------------------- + # Creating connected sockets + #-------------------------------------------------------------------------- + + def _make_url(self, channel): + """Make a ZeroMQ URL for a given channel.""" + transport = self.transport + ip = self.ip + port = getattr(self, '%s_port' % channel) + + if transport == 'tcp': + return "tcp://%s:%i" % (ip, port) + else: + return "%s://%s-%s" % (transport, ip, port) + + def _create_connected_socket(self, channel, identity=None): + """Create a zmq Socket and connect it to the kernel.""" + url = self._make_url(channel) + socket_type = channel_socket_types[channel] + self.log.info("Connecting to: %s" % url) + sock = self.context.socket(socket_type) + if identity: + sock.identity = identity + sock.connect(url) + return sock + + def connect_iopub(self, identity=None): + """return zmq Socket connected to the IOPub channel""" + sock = self._create_connected_socket('iopub', identity=identity) + sock.setsockopt(zmq.SUBSCRIBE, b'') + return sock + + def connect_shell(self, identity=None): + """return zmq Socket connected to the Shell channel""" + return self._create_connected_socket('shell', identity=identity) + + def connect_stdin(self, identity=None): + """return zmq Socket connected to the StdIn channel""" + return self._create_connected_socket('stdin', identity=identity) + + def connect_hb(self, identity=None): + """return zmq Socket connected to the Heartbeat channel""" + return self._create_connected_socket('hb', identity=identity) + + def connect_control(self, identity=None): + """return zmq Socket connected to the Heartbeat channel""" + return self._create_connected_socket('control', identity=identity) __all__ = [ diff --git a/IPython/kernel/manager.py b/IPython/kernel/manager.py index add6664..cd30195 100644 --- a/IPython/kernel/manager.py +++ b/IPython/kernel/manager.py @@ -42,14 +42,6 @@ from .managerabc import ( # Main kernel manager class #----------------------------------------------------------------------------- -_socket_types = { - 'hb' : zmq.REQ, - 'shell' : zmq.DEALER, - 'iopub' : zmq.SUB, - 'stdin' : zmq.DEALER, - 'control': zmq.DEALER, -} - class KernelManager(LoggingConfigurable, ConnectionFileMixin): """Manages a single kernel in a subprocess on this host. @@ -145,54 +137,6 @@ class KernelManager(LoggingConfigurable, ConnectionFileMixin): return self.client_factory(**kw) #-------------------------------------------------------------------------- - # Connection info - #-------------------------------------------------------------------------- - - def _make_url(self, channel): - """Make a ZeroMQ URL for a given channel.""" - transport = self.transport - ip = self.ip - port = getattr(self, '%s_port' % channel) - - if transport == 'tcp': - return "tcp://%s:%i" % (ip, port) - else: - return "%s://%s-%s" % (transport, ip, port) - - def _create_connected_socket(self, channel, identity=None): - """Create a zmq Socket and connect it to the kernel.""" - url = self._make_url(channel) - socket_type = _socket_types[channel] - self.log.info("Connecting to: %s" % url) - sock = self.context.socket(socket_type) - if identity: - sock.identity = identity - sock.connect(url) - return sock - - def connect_iopub(self, identity=None): - """return zmq Socket connected to the IOPub channel""" - sock = self._create_connected_socket('iopub', identity=identity) - sock.setsockopt(zmq.SUBSCRIBE, b'') - return sock - - def connect_shell(self, identity=None): - """return zmq Socket connected to the Shell channel""" - return self._create_connected_socket('shell', identity=identity) - - def connect_stdin(self, identity=None): - """return zmq Socket connected to the StdIn channel""" - return self._create_connected_socket('stdin', identity=identity) - - def connect_hb(self, identity=None): - """return zmq Socket connected to the Heartbeat channel""" - return self._create_connected_socket('hb', identity=identity) - - def connect_control(self, identity=None): - """return zmq Socket connected to the Heartbeat channel""" - return self._create_connected_socket('control', identity=identity) - - #-------------------------------------------------------------------------- # Kernel management #--------------------------------------------------------------------------