Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5e7eec7
Implement blocking channels without Python threads
takluyver Dec 3, 2014
2dfb452
Collapse Qt out of process channel class hierarchy
takluyver Dec 3, 2014
a023d9b
Merge Qt*ChannelMixin up into QtInProcess*Channel classes
takluyver Dec 3, 2014
868cb7a
Factor out code to set up ZMQ sockets
takluyver Dec 4, 2014
d99901f
Remove some dead code
takluyver Dec 4, 2014
71644c2
Use messaging methods on kernel client instead of channel objects
takluyver Dec 4, 2014
7f6f089
Use client.execute in execute preoprocessor
takluyver Dec 4, 2014
03729e7
Move methods to send specific messages from channels to KernelClient
takluyver Dec 4, 2014
d13d384
Move kernel_info for adaptation onto KernelClient
takluyver Dec 4, 2014
956e44b
Move ZMQ socket creation out of channels
takluyver Dec 4, 2014
afabf29
Remove now-unused code
takluyver Dec 4, 2014
59444d6
Get rid of unused Qt signals for specific message types
takluyver Dec 4, 2014
9fb8d97
Get rid of Qt Shell and Stdin channels
takluyver Dec 4, 2014
af8fe49
Get rid of QtIOPubChannel class
takluyver Dec 4, 2014
e695ea3
Collapse Qt in-process channel classes together
takluyver Dec 4, 2014
abf4ddf
Collapse inprocess channel classes together
takluyver Dec 4, 2014
414d3ae
Share an IOLoop among Qt channels, rather than one each
takluyver Dec 4, 2014
f698e96
Get rid of address property on channels
takluyver Dec 4, 2014
60fe687
Fix attribute naming
takluyver Dec 4, 2014
11deefc
Simplify HBChannel inheritance
takluyver Dec 5, 2014
34c4251
Collapse ZMQSocketChannel into HBChannel class
takluyver Dec 5, 2014
4064ddd
Remove unused validate_string_list function
takluyver Dec 5, 2014
d445184
Collapse ChannelQObject class into QtInProcessChannel
takluyver Dec 5, 2014
cc0350f
Restore QtInProcessHBChannel class
takluyver Dec 5, 2014
9f8f75a
Improve docstring for InProcessHBChannel
takluyver Dec 5, 2014
e59addc
Ensure console widget object always has _display_banner attribute
takluyver Dec 5, 2014
d12d89f
Remove unused channel ABCs
takluyver Dec 5, 2014
5b22c40
Remove unused copies of validate_string_dict
takluyver Dec 5, 2014
a060c26
Rename _queue_send to send
takluyver Dec 8, 2014
564961b
Use modern pyzmq attribute cleverness
takluyver Dec 8, 2014
99c7bd8
Remove redundant make_*_socket functions
takluyver Dec 11, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 59 additions & 52 deletions IPython/kernel/blocking/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,59 @@
Useful for test suites and blocking terminal interfaces.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2013 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

try:
from queue import Queue, Empty # Py 3
except ImportError:
from Queue import Queue, Empty # Py 2

from IPython.kernel.channels import IOPubChannel, HBChannel, \
ShellChannel, StdInChannel

#-----------------------------------------------------------------------------
# Blocking kernel manager
#-----------------------------------------------------------------------------


class BlockingChannelMixin(object):

def __init__(self, *args, **kwds):
super(BlockingChannelMixin, self).__init__(*args, **kwds)
self._in_queue = Queue()

def call_handlers(self, msg):
self._in_queue.put(msg)
class ZMQSocketChannel(object):
"""A ZMQ socket in a simple blocking API"""
session = None
socket = None
stream = None
_exiting = False
proxy_methods = []

def __init__(self, socket, session, loop=None):
"""Create a channel.
Parameters
----------
socket : :class:`zmq.Socket`
The ZMQ socket to use.
session : :class:`session.Session`
The session to use.
loop
Unused here, for other implementations
"""
super(ZMQSocketChannel, self).__init__()

self.socket = socket
self.session = session

def _recv(self, **kwargs):
msg = self.socket.recv_multipart(**kwargs)
ident,smsg = self.session.feed_identities(msg)
return self.session.deserialize(smsg)

def get_msg(self, block=True, timeout=None):
""" Gets a message if there is one that is ready. """
if timeout is None:
# Queue.get(timeout=None) has stupid uninteruptible
# behavior, so wait for a week instead
timeout = 604800
return self._in_queue.get(block, timeout)
if block:
if timeout is not None:
timeout *= 1000 # seconds to ms
ready = self.socket.poll(timeout)
else:
ready = self.socket.poll(timeout=0)

if ready:
return self._recv()
else:
raise Empty

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

def msg_ready(self):
""" Is there a message that has been received? """
return not self._in_queue.empty()


class BlockingIOPubChannel(BlockingChannelMixin, IOPubChannel):
pass


class BlockingShellChannel(BlockingChannelMixin, ShellChannel):
def call_handlers(self, msg):
if msg['msg_type'] == 'kernel_info_reply':
self._handle_kernel_info_reply(msg)
return super(BlockingShellChannel, self).call_handlers(msg)


class BlockingStdInChannel(BlockingChannelMixin, StdInChannel):
pass
return bool(self.socket.poll(timeout=0))

def close(self):
if self.socket is not None:
try:
self.socket.close(linger=0)
except Exception:
pass
self.socket = None
stop = close

class BlockingHBChannel(HBChannel):
def is_alive(self):
return (self.socket is not None)

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

def call_handlers(self, since_last_heartbeat):
""" Pause beating on missed heartbeat. """
def start(self):
pass

48 changes: 27 additions & 21 deletions IPython/kernel/blocking/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@
Useful for test suites and blocking terminal interfaces.
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2013 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
try:
from queue import Empty # Python 3
except ImportError:
from Queue import Empty # Python 2

from IPython.utils.traitlets import Type
from IPython.kernel.channels import HBChannel
from IPython.kernel.client import KernelClient
from .channels import (
BlockingIOPubChannel, BlockingHBChannel,
BlockingShellChannel, BlockingStdInChannel
)

#-----------------------------------------------------------------------------
# Blocking kernel manager
#-----------------------------------------------------------------------------
from .channels import ZMQSocketChannel

class BlockingKernelClient(KernelClient):
def wait_for_ready(self):
# Wait for kernel info reply on shell channel
while True:
msg = self.shell_channel.get_msg(block=True)
if msg['msg_type'] == 'kernel_info_reply':
self._handle_kernel_info_reply(msg)
break

# Flush IOPub channel
while True:
try:
msg = self.iopub_channel.get_msg(block=True, timeout=0.2)
print(msg['msg_type'])
except Empty:
break

# The classes to use for the various channels
shell_channel_class = Type(BlockingShellChannel)
iopub_channel_class = Type(BlockingIOPubChannel)
stdin_channel_class = Type(BlockingStdInChannel)
hb_channel_class = Type(BlockingHBChannel)
shell_channel_class = Type(ZMQSocketChannel)
iopub_channel_class = Type(ZMQSocketChannel)
stdin_channel_class = Type(ZMQSocketChannel)
hb_channel_class = Type(HBChannel)
Loading