Skip to content

Commit fe5dd27

Browse files
committed
Merge pull request #7108 from takluyver/digging-channels
Refactor kernel channel classes
2 parents 4a30114 + 99c7bd8 commit fe5dd27

22 files changed

Lines changed: 800 additions & 989 deletions

‎IPython/kernel/blocking/channels.py‎

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,59 @@
22
33
Useful for test suites and blocking terminal interfaces.
44
"""
5-
#-----------------------------------------------------------------------------
6-
# Copyright (C) 2013 The IPython Development Team
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-
#-----------------------------------------------------------------------------
115

12-
#-----------------------------------------------------------------------------
13-
# Imports
14-
#-----------------------------------------------------------------------------
6+
# Copyright (c) IPython Development Team.
7+
# Distributed under the terms of the Modified BSD License.
158

169
try:
1710
from queue import Queue, Empty # Py 3
1811
except ImportError:
1912
from Queue import Queue, Empty # Py 2
2013

21-
from IPython.kernel.channels import IOPubChannel, HBChannel, \
22-
ShellChannel, StdInChannel
2314

24-
#-----------------------------------------------------------------------------
25-
# Blocking kernel manager
26-
#-----------------------------------------------------------------------------
27-
28-
29-
class BlockingChannelMixin(object):
30-
31-
def __init__(self, *args, **kwds):
32-
super(BlockingChannelMixin, self).__init__(*args, **kwds)
33-
self._in_queue = Queue()
34-
35-
def call_handlers(self, msg):
36-
self._in_queue.put(msg)
15+
class ZMQSocketChannel(object):
16+
"""A ZMQ socket in a simple blocking API"""
17+
session = None
18+
socket = None
19+
stream = None
20+
_exiting = False
21+
proxy_methods = []
22+
23+
def __init__(self, socket, session, loop=None):
24+
"""Create a channel.
25+
26+
Parameters
27+
----------
28+
socket : :class:`zmq.Socket`
29+
The ZMQ socket to use.
30+
session : :class:`session.Session`
31+
The session to use.
32+
loop
33+
Unused here, for other implementations
34+
"""
35+
super(ZMQSocketChannel, self).__init__()
36+
37+
self.socket = socket
38+
self.session = session
39+
40+
def _recv(self, **kwargs):
41+
msg = self.socket.recv_multipart(**kwargs)
42+
ident,smsg = self.session.feed_identities(msg)
43+
return self.session.deserialize(smsg)
3744

3845
def get_msg(self, block=True, timeout=None):
3946
""" Gets a message if there is one that is ready. """
40-
if timeout is None:
41-
# Queue.get(timeout=None) has stupid uninteruptible
42-
# behavior, so wait for a week instead
43-
timeout = 604800
44-
return self._in_queue.get(block, timeout)
47+
if block:
48+
if timeout is not None:
49+
timeout *= 1000 # seconds to ms
50+
ready = self.socket.poll(timeout)
51+
else:
52+
ready = self.socket.poll(timeout=0)
53+
54+
if ready:
55+
return self._recv()
56+
else:
57+
raise Empty
4558

4659
def get_msgs(self):
4760
""" Get all messages that are currently ready. """
@@ -55,31 +68,25 @@ def get_msgs(self):
5568

5669
def msg_ready(self):
5770
""" Is there a message that has been received? """
58-
return not self._in_queue.empty()
59-
60-
61-
class BlockingIOPubChannel(BlockingChannelMixin, IOPubChannel):
62-
pass
63-
64-
65-
class BlockingShellChannel(BlockingChannelMixin, ShellChannel):
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)
70-
71-
72-
class BlockingStdInChannel(BlockingChannelMixin, StdInChannel):
73-
pass
71+
return bool(self.socket.poll(timeout=0))
7472

73+
def close(self):
74+
if self.socket is not None:
75+
try:
76+
self.socket.close(linger=0)
77+
except Exception:
78+
pass
79+
self.socket = None
80+
stop = close
7581

76-
class BlockingHBChannel(HBChannel):
82+
def is_alive(self):
83+
return (self.socket is not None)
7784

78-
# This kernel needs quicker monitoring, shorten to 1 sec.
79-
# less than 0.5s is unreliable, and will get occasional
80-
# false reports of missed beats.
81-
time_to_dead = 1.
85+
def send(self, msg):
86+
"""Pass a message to the ZMQ socket to send
87+
"""
88+
self.session.send(self.socket, msg)
8289

83-
def call_handlers(self, since_last_heartbeat):
84-
""" Pause beating on missed heartbeat. """
90+
def start(self):
8591
pass
92+

‎IPython/kernel/blocking/client.py‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@
22
33
Useful for test suites and blocking terminal interfaces.
44
"""
5-
#-----------------------------------------------------------------------------
6-
# Copyright (C) 2013 The IPython Development Team
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-
#-----------------------------------------------------------------------------
5+
# Copyright (c) IPython Development Team.
6+
# Distributed under the terms of the Modified BSD License.
117

12-
#-----------------------------------------------------------------------------
13-
# Imports
14-
#-----------------------------------------------------------------------------
8+
try:
9+
from queue import Empty # Python 3
10+
except ImportError:
11+
from Queue import Empty # Python 2
1512

1613
from IPython.utils.traitlets import Type
14+
from IPython.kernel.channels import HBChannel
1715
from IPython.kernel.client import KernelClient
18-
from .channels import (
19-
BlockingIOPubChannel, BlockingHBChannel,
20-
BlockingShellChannel, BlockingStdInChannel
21-
)
22-
23-
#-----------------------------------------------------------------------------
24-
# Blocking kernel manager
25-
#-----------------------------------------------------------------------------
16+
from .channels import ZMQSocketChannel
2617

2718
class BlockingKernelClient(KernelClient):
19+
def wait_for_ready(self):
20+
# Wait for kernel info reply on shell channel
21+
while True:
22+
msg = self.shell_channel.get_msg(block=True)
23+
if msg['msg_type'] == 'kernel_info_reply':
24+
self._handle_kernel_info_reply(msg)
25+
break
26+
27+
# Flush IOPub channel
28+
while True:
29+
try:
30+
msg = self.iopub_channel.get_msg(block=True, timeout=0.2)
31+
print(msg['msg_type'])
32+
except Empty:
33+
break
2834

2935
# The classes to use for the various channels
30-
shell_channel_class = Type(BlockingShellChannel)
31-
iopub_channel_class = Type(BlockingIOPubChannel)
32-
stdin_channel_class = Type(BlockingStdInChannel)
33-
hb_channel_class = Type(BlockingHBChannel)
36+
shell_channel_class = Type(ZMQSocketChannel)
37+
iopub_channel_class = Type(ZMQSocketChannel)
38+
stdin_channel_class = Type(ZMQSocketChannel)
39+
hb_channel_class = Type(HBChannel)

0 commit comments

Comments
 (0)