1 from threading import Lock
4 __init__ (self, waker):
5 self._state_to_set = None
6 self._state_last_set = None
8 self.set_state = ThreadFunction(self._set_state, "Space API")
9 self._set_state_lock = Lock()
10 waker.register(self.set_state, 10.0) # re-try setting the state every 10 seconds
15 # set_state is the asynchronous version of _set_state (see __init__)
16 def _set_state (self, state = None):
17 '''Sets the state, if None: leave state unchanged and re-try if previous attempts failed'''
19 self._state_to_set = state
21 # always have a local variable because of parallelism
22 state = self._state_to_set
24 if not self._set_state_lock.acquire(False):
25 # we don't want many threads to wait here
26 # the next status update will fix everything anyways
31 # check if there's something we need to do
32 if self._state_last_set == state: return
34 error = do_request(stts) # TODO
36 #TODO error too often -> log critical to send mails
38 self.state_last_set = stts
40 self._set_state_lock.release()