From 956e44b859be04f335c9f91e510f429c1ba198bf 2014-12-05 01:48:48 From: Thomas Kluyver Date: 2014-12-05 01:48:48 Subject: [PATCH] Move ZMQ socket creation out of channels --- diff --git a/IPython/kernel/blocking/channels.py b/IPython/kernel/blocking/channels.py index 36bb78a..5f3c568 100644 --- a/IPython/kernel/blocking/channels.py +++ b/IPython/kernel/blocking/channels.py @@ -54,7 +54,7 @@ class ZMQSocketChannel(object): _exiting = False proxy_methods = [] - def __init__(self, context, session, address): + def __init__(self, socket, session): """Create a channel. Parameters @@ -69,14 +69,8 @@ class ZMQSocketChannel(object): super(ZMQSocketChannel, self).__init__() self.daemon = True - self.context = context + self.socket = socket self.session = session - if isinstance(address, tuple): - if address[1] == 0: - message = 'The port number for a channel cannot be 0.' - raise InvalidPortNumber(message) - address = "tcp://%s:%i" % address - self._address = address def _recv(self, **kwargs): msg = self.socket.recv_multipart(**kwargs) @@ -136,6 +130,9 @@ class ZMQSocketChannel(object): """ self.session.send(self.socket, msg) + def start(self): + pass + class BlockingShellChannel(ZMQSocketChannel): """The shell channel for issuing request/replies to the kernel.""" diff --git a/IPython/kernel/blocking/client.py b/IPython/kernel/blocking/client.py index c2c7496..ddcbcb1 100644 --- a/IPython/kernel/blocking/client.py +++ b/IPython/kernel/blocking/client.py @@ -12,10 +12,7 @@ except ImportError: from IPython.utils.traitlets import Type from IPython.kernel.client import KernelClient -from .channels import ( - BlockingIOPubChannel, BlockingHBChannel, - BlockingShellChannel, BlockingStdInChannel -) +from .channels import ZMQSocketChannel, BlockingHBChannel class BlockingKernelClient(KernelClient): def wait_for_ready(self): @@ -35,7 +32,7 @@ class BlockingKernelClient(KernelClient): break # The classes to use for the various channels - shell_channel_class = Type(BlockingShellChannel) - iopub_channel_class = Type(BlockingIOPubChannel) - stdin_channel_class = Type(BlockingStdInChannel) + shell_channel_class = Type(ZMQSocketChannel) + iopub_channel_class = Type(ZMQSocketChannel) + stdin_channel_class = Type(ZMQSocketChannel) hb_channel_class = Type(BlockingHBChannel) diff --git a/IPython/kernel/client.py b/IPython/kernel/client.py index 729a331..cd77320 100644 --- a/IPython/kernel/client.py +++ b/IPython/kernel/client.py @@ -16,6 +16,9 @@ from IPython.utils.traitlets import ( from .channelsabc import ( ShellChannelABC, IOPubChannelABC, HBChannelABC, StdInChannelABC ) +from .channels import ( + make_shell_socket, make_stdin_socket, make_iopub_socket +) from .clientabc import KernelClientABC from .connect import ConnectionFileMixin @@ -127,8 +130,9 @@ class KernelClient(ConnectionFileMixin): if self._shell_channel is None: url = self._make_url('shell') self.log.debug("connecting shell channel to %s", url) + socket = make_shell_socket(self.context, self.session.bsession, url) self._shell_channel = self.shell_channel_class( - self.context, self.session, url + socket, self.session ) return self._shell_channel @@ -138,8 +142,9 @@ class KernelClient(ConnectionFileMixin): if self._iopub_channel is None: url = self._make_url('iopub') self.log.debug("connecting iopub channel to %s", url) + socket = make_iopub_socket(self.context, self.session.bsession, url) self._iopub_channel = self.iopub_channel_class( - self.context, self.session, url + socket, self.session ) return self._iopub_channel @@ -149,8 +154,9 @@ class KernelClient(ConnectionFileMixin): if self._stdin_channel is None: url = self._make_url('stdin') self.log.debug("connecting stdin channel to %s", url) + socket = make_stdin_socket(self.context, self.session.bsession, url) self._stdin_channel = self.stdin_channel_class( - self.context, self.session, url + socket, self.session ) return self._stdin_channel diff --git a/IPython/qt/client.py b/IPython/qt/client.py index d4deb73..f6c9841 100644 --- a/IPython/qt/client.py +++ b/IPython/qt/client.py @@ -55,12 +55,10 @@ def validate_string_dict(dct): class QtZMQSocketChannel(SuperQObject, Thread): """The base class for the channels that use ZMQ sockets.""" - context = None session = None socket = None ioloop = None stream = None - _address = None _exiting = False proxy_methods = [] @@ -87,7 +85,7 @@ class QtZMQSocketChannel(SuperQObject, Thread): """ QtCore.QCoreApplication.instance().processEvents() - def __init__(self, context, session, address): + def __init__(self, socket, session): """Create a channel. Parameters @@ -102,14 +100,8 @@ class QtZMQSocketChannel(SuperQObject, Thread): super(QtZMQSocketChannel, self).__init__() self.daemon = True - self.context = context + self.socket = socket self.session = session - if isinstance(address, tuple): - if address[1] == 0: - message = 'The port number for a channel cannot be 0.' - raise InvalidPortNumber(message) - address = "tcp://%s:%i" % address - self._address = address atexit.register(self._notice_exit) def _notice_exit(self): @@ -218,13 +210,12 @@ class QtShellChannel(QtZMQSocketChannel): history_reply = QtCore.Signal(object) kernel_info_reply = QtCore.Signal(object) - def __init__(self, context, session, address): - super(QtShellChannel, self).__init__(context, session, address) + def __init__(self, socket, session): + super(QtShellChannel, self).__init__(socket, session) self.ioloop = ioloop.IOLoop() def run(self): """The thread's main activity. Call start() instead.""" - self.socket = make_shell_socket(self.context, self.session.bsession, self.address) self.stream = zmqstream.ZMQStream(self.socket, self.ioloop) self.stream.on_recv(self._handle_recv) self._run_loop() @@ -279,13 +270,12 @@ class QtIOPubChannel(QtZMQSocketChannel): # Emitted when a shutdown is noticed. shutdown_reply_received = QtCore.Signal(object) - def __init__(self, context, session, address): - super(QtIOPubChannel, self).__init__(context, session, address) + def __init__(self, socket, session): + super(QtIOPubChannel, self).__init__(socket, session) self.ioloop = ioloop.IOLoop() def run(self): """The thread's main activity. Call start() instead.""" - self.socket = make_iopub_socket(self.context, self.session.bsession, self.address) self.stream = zmqstream.ZMQStream(self.socket, self.ioloop) self.stream.on_recv(self._handle_recv) self._run_loop() @@ -338,13 +328,12 @@ class QtStdInChannel(QtZMQSocketChannel): # Emitted when an input request is received. input_requested = QtCore.Signal(object) - def __init__(self, context, session, address): - super(QtStdInChannel, self).__init__(context, session, address) + def __init__(self, socket, session): + super(QtStdInChannel, self).__init__(socket, session) self.ioloop = ioloop.IOLoop() def run(self): """The thread's main activity. Call start() instead.""" - self.socket = make_stdin_socket(self.context, self.session.bsession, self.address) self.stream = zmqstream.ZMQStream(self.socket, self.ioloop) self.stream.on_recv(self._handle_recv) self._run_loop()