def _(): stm.datatypes.TObject.__init__(self) self._tasks = stm.datatypes.TList() self._max_threads = max_threads self._keep_alive = keep_alive self._live_threads = 0 self._tasks_scheduled = 0 self._tasks_finished = 0 # Hack until I write a proper TSet datatype self._free_threads = stm.datatypes.TDict() stm.watch(self._calculate_threads_short, self._spin_up_threads) stm.watch(self._check_tasks_ready, self._allocate_tasks)
def atomically_watch(function, callback=None): """ A wrapper around stm.watch that automatically runs the call inside a transaction. This is essentially equivalent to:: stm.atomically(lambda: stm.watch(function, callback)) but, as with stm.watch, atomically_watch can be used as a decorator by omitting the callback parameter. For example, the following could be used outside of a transaction to place a new watch:: @atomically_watch(some_tvar.get) def _(result): ...do something... This would be equivalent to: @stm.atomically def _(): @stm.watch(some_tvar.get) def _(result): ..do something.. Note that the callback will (as callbacks always are) still be run inside a transaction. If you need to perform I/O in the callback, use stm.eventloop.scheduled_function to decorate the callback such that it will be run by the event loop outside of the scope of STM:: @atomically_watch(some_tvar.get) @stm.eventloop.scheduled_function def _(result): print "Changed to " + str(result) # Or any other I/O """ if callback is None: def decorator(actual_callback): atomically_watch(function, actual_callback) return decorator stm.atomically(lambda: stm.watch(function, callback))