##// END OF EJS Templates
Use python timers instead of thread + sleep
Use python timers instead of thread + sleep

File last commit:

r9371:8c06dce5
r10382:bcdeb79b
Show More
blockingkernelmanager.py
90 lines | 2.8 KiB | text/x-python | PythonLexer
/ IPython / kernel / blockingkernelmanager.py
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 """ Implements a fully blocking kernel manager.
Fernando Perez
Rework messaging to better conform to our spec....
r2926
Useful for test suites and blocking terminal interfaces.
"""
#-----------------------------------------------------------------------------
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 # Copyright (C) 2010-2012 The IPython Development Team
Fernando Perez
Rework messaging to better conform to our spec....
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
Creating an ABC for kernel managers and channels.
r9121 import Queue
Fernando Perez
Rework messaging to better conform to our spec....
r2926 from IPython.utils.traitlets import Type
MinRK
move multikernelmanager to IPython.kernel
r9371 from .kernelmanager import KernelManager, IOPubChannel, HBChannel, \
Brian Granger
Cleanup naming and organization of channels....
r9120 ShellChannel, StdInChannel
Brian Granger
Added initial draft of blockingkernelmanager.py.
r2693
Fernando Perez
Rework messaging to better conform to our spec....
r2926 #-----------------------------------------------------------------------------
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 # Blocking kernel manager
Fernando Perez
Rework messaging to better conform to our spec....
r2926 #-----------------------------------------------------------------------------
Brian Granger
Added initial draft of blockingkernelmanager.py.
r2693
Brian Granger
Cleanup naming and organization of channels....
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
never use Queue.get(timeout=None)...
r9358 if timeout is None:
# Queue.get(timeout=None) has stupid uninteruptible
# behavior, so wait for a week instead
timeout = 604800
Brian Granger
Cleanup naming and organization of channels....
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
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 pass
Fernando Perez
Rework messaging to better conform to our spec....
r2926
Brian Granger
Cleanup naming and organization of channels....
r9120
class BlockingShellChannel(BlockingChannelMixin, ShellChannel):
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 pass
Fernando Perez
Rework messaging to better conform to our spec....
r2926
Brian Granger
Cleanup naming and organization of channels....
r9120
class BlockingStdInChannel(BlockingChannelMixin, StdInChannel):
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 pass
Fernando Perez
Rework messaging to better conform to our spec....
r2926
Brian Granger
Cleanup naming and organization of channels....
r9120
class BlockingHBChannel(HBChannel):
epatters
Add missing msg queue + comment out debug printing in BlockingKernelManager.
r3825
MinRK
Fixes to the heartbeat channel...
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
Rework messaging to better conform to our spec....
r2926
def call_handlers(self, since_last_heartbeat):
epatters
Refactor kernel managers in preparation for the EmbeddedKernel.
r8408 """ Pause beating on missed heartbeat. """
epatters
Add missing msg queue + comment out debug printing in BlockingKernelManager.
r3825 pass
Brian Granger
Cleanup naming and organization of channels....
r9120
Fernando Perez
Rework messaging to better conform to our spec....
r2926 class BlockingKernelManager(KernelManager):
# The classes to use for the various channels.
Brian Granger
Cleanup naming and organization of channels....
r9120 shell_channel_class = Type(BlockingShellChannel)
iopub_channel_class = Type(BlockingIOPubChannel)
stdin_channel_class = Type(BlockingStdInChannel)
hb_channel_class = Type(BlockingHBChannel)
Brian Granger
Creating an ABC for kernel managers and channels.
r9121