channels.py
79 lines
| 2.4 KiB
| text/x-python
|
PythonLexer
MinRK
|
r10285 | """Blocking channels | ||
Fernando Perez
|
r2926 | |||
Useful for test suites and blocking terminal interfaces. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r10285 | # Copyright (C) 2013 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 | ||
MinRK
|
r10285 | from IPython.kernel.channels import 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): | ||||
MinRK
|
r10283 | |||
Brian Granger
|
r9120 | def __init__(self, *args, **kwds): | ||
super(BlockingChannelMixin, self).__init__(*args, **kwds) | ||||
self._in_queue = Queue.Queue() | ||||
MinRK
|
r10283 | |||
Brian Granger
|
r9120 | def call_handlers(self, msg): | ||
self._in_queue.put(msg) | ||||
MinRK
|
r10283 | |||
Brian Granger
|
r9120 | 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) | ||
MinRK
|
r10283 | |||
Brian Granger
|
r9120 | 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 | ||||
MinRK
|
r10283 | |||
Brian Granger
|
r9120 | 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): | ||||
MinRK
|
r10283 | |||
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 | ||