Extends a Runnable subclass with a stop_signal semaphore/event, and amends the existing halt and next methods to signal stop via the semaphore, and reset the stop signal on run completion, respectively.


A Runnable block that accepts timeout and on run blocks for up to timeout seconds. It can be interrupted via call to stop, in which case blocks shorter than the timeout interval:

class StoppableSleeper(Runnable):
    def next(self, state, timeout=None, **runopts):
        return state

>>> sleeper = StoppableSleeper(timeout=30)
>>> sleeper.run(state)
<Future at 0x7fbb575e6f60 state=running>

>>> sleeper.stop()
>>> sleeper.timers
    'dispatch.next': [13.224211193970405]