| 1 |
|
|---|
| 2 | def delayed_reaction(session, initiator_func, initiator_args, ready_test_func,
|
|---|
| 3 | final_func, final_func_args):
|
|---|
| 4 | '''
|
|---|
| 5 | Designed to work together with threaded (Python or C++) objects, to
|
|---|
| 6 | start a long-running threaded task and then automatically apply the
|
|---|
| 7 | result (in a later GUI update) when done.
|
|---|
| 8 | Args:
|
|---|
| 9 | initiator_func: A handle to the function that kicks off the threaded
|
|---|
| 10 | process. Should not return anything.
|
|---|
| 11 | initiator_args: A tuple of arguments to be applied to initiator_func
|
|---|
| 12 | ready_test_func: Should return True when the threaded task is done,
|
|---|
| 13 | false otherwise
|
|---|
| 14 | final_func: Task to run once the thread is done. It should already know
|
|---|
| 15 | how to find the thread result
|
|---|
| 16 | final_func_args: A tuple of arguments to be applied to final_func
|
|---|
| 17 | (e.g. to tell it what to do with the result)
|
|---|
| 18 | '''
|
|---|
| 19 | initiator_func(*initiator_args)
|
|---|
| 20 | class _cb:
|
|---|
| 21 | def __init__(self, session, ready_test_func, final_func, final_func_args):
|
|---|
| 22 | self.session = session
|
|---|
| 23 | self.tf = ready_test_func
|
|---|
| 24 | self.ff = final_func
|
|---|
| 25 | self.ff_args = final_func_args
|
|---|
| 26 | self.handler = session.triggers.add_handler('new frame', self.callback)
|
|---|
| 27 | def callback(self, *_):
|
|---|
| 28 | if self.tf():
|
|---|
| 29 | self.ff(*self.ff_args)
|
|---|
| 30 | self.session.triggers.remove_handler(self.handler)
|
|---|
| 31 | cb = _cb(session, ready_test_func, final_func, final_func_args)
|
|---|
| 32 |
|
|---|
| 33 | class Delayed_Reaction_Tester:
|
|---|
| 34 | def __init__(self, frame_delay):
|
|---|
| 35 | self._counter = 0
|
|---|
| 36 | self._frame_delay = frame_delay
|
|---|
| 37 |
|
|---|
| 38 | def initiator_func(self):
|
|---|
| 39 | pass
|
|---|
| 40 |
|
|---|
| 41 | def ready_test_func(self):
|
|---|
| 42 | self._counter += 1
|
|---|
| 43 | if (self._counter >= self._frame_delay):
|
|---|
| 44 | return True
|
|---|
| 45 | return False
|
|---|
| 46 |
|
|---|
| 47 | def final_func(self, to_print):
|
|---|
| 48 | print(to_print)
|
|---|