blockingkernelmanager.py
90 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
epatters
|
r8408 | """ Implements a fully blocking kernel manager. | ||
Fernando Perez
|
r2926 | |||
Useful for test suites and blocking terminal interfaces. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
epatters
|
r8408 | # Copyright (C) 2010-2012 The IPython Development Team | ||
Fernando Perez
|
r2926 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING.txt, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r9121 | import Queue | ||
Fernando Perez
|
r2926 | from IPython.utils.traitlets import Type | ||
MinRK
|
r9371 | from .kernelmanager import KernelManager, IOPubChannel, HBChannel, \ | ||
Brian Granger
|
r9120 | ShellChannel, StdInChannel | ||
Brian Granger
|
r2693 | |||
Fernando Perez
|
r2926 | #----------------------------------------------------------------------------- | ||
epatters
|
r8408 | # Blocking kernel manager | ||
Fernando Perez
|
r2926 | #----------------------------------------------------------------------------- | ||
Brian Granger
|
r2693 | |||
Brian Granger
|
r9120 | |||
class BlockingChannelMixin(object): | ||||
def __init__(self, *args, **kwds): | ||||
super(BlockingChannelMixin, self).__init__(*args, **kwds) | ||||
self._in_queue = Queue.Queue() | ||||
def call_handlers(self, msg): | ||||
self._in_queue.put(msg) | ||||
def get_msg(self, block=True, timeout=None): | ||||
""" Gets a message if there is one that is ready. """ | ||||
MinRK
|
r9358 | if timeout is None: | ||
# Queue.get(timeout=None) has stupid uninteruptible | ||||
# behavior, so wait for a week instead | ||||
timeout = 604800 | ||||
Brian Granger
|
r9120 | return self._in_queue.get(block, timeout) | ||
def get_msgs(self): | ||||
""" Get all messages that are currently ready. """ | ||||
msgs = [] | ||||
while True: | ||||
try: | ||||
msgs.append(self.get_msg(block=False)) | ||||
except Queue.Empty: | ||||
break | ||||
return msgs | ||||
def msg_ready(self): | ||||
""" Is there a message that has been received? """ | ||||
return not self._in_queue.empty() | ||||
class BlockingIOPubChannel(BlockingChannelMixin, IOPubChannel): | ||||
epatters
|
r8408 | pass | ||
Fernando Perez
|
r2926 | |||
Brian Granger
|
r9120 | |||
class BlockingShellChannel(BlockingChannelMixin, ShellChannel): | ||||
epatters
|
r8408 | pass | ||
Fernando Perez
|
r2926 | |||
Brian Granger
|
r9120 | |||
class BlockingStdInChannel(BlockingChannelMixin, StdInChannel): | ||||
epatters
|
r8408 | pass | ||
Fernando Perez
|
r2926 | |||
Brian Granger
|
r9120 | |||
class BlockingHBChannel(HBChannel): | ||||
epatters
|
r3825 | |||
MinRK
|
r5614 | # 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. | ||||
Fernando Perez
|
r2926 | |||
def call_handlers(self, since_last_heartbeat): | ||||
epatters
|
r8408 | """ Pause beating on missed heartbeat. """ | ||
epatters
|
r3825 | pass | ||
Brian Granger
|
r9120 | |||
Fernando Perez
|
r2926 | class BlockingKernelManager(KernelManager): | ||
# The classes to use for the various channels. | ||||
Brian Granger
|
r9120 | shell_channel_class = Type(BlockingShellChannel) | ||
iopub_channel_class = Type(BlockingIOPubChannel) | ||||
stdin_channel_class = Type(BlockingStdInChannel) | ||||
hb_channel_class = Type(BlockingHBChannel) | ||||
Brian Granger
|
r9121 | |||