hybrid.core.stoppable#

stoppable(cls)[source]#

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.

Example

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:

@stoppable
class StoppableSleeper(Runnable):
    def next(self, state, timeout=None, **runopts):
        self.stop_signal.wait(timeout=timeout)
        return state

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

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