1 from libtuer import ThreadRepeater
2 from threading import Lock
7 '''a simple struct storing information about a to-be-woken function'''
8 def __init__(self, f, period, one_shot):
11 self.time_since_call = 0
12 self.one_shot = one_shot
15 def __init__(self, sm):
17 self._t = ThreadRepeater(self._wake, SLEEP_TIME, name="Waker")
19 self._tobewokens_lock = Lock()
21 def register(f, time, one_shot = False):
22 '''Register a function which is called approximately every <time> seconds (or just once, if one_shot is True). f should return quickly, or it will delay the waker!'''
23 time = max(time//SLEEP_TIME, 1)
24 with self._tobewokens_lock:
25 self._tobewokens.append(ToBeWoken(f, time, 0, one_shot))
28 with self._tobewokens_lock:
30 # run the functions we ought to run
31 for tobewoken, idx in zip(self._tobewokens, range(len(self._tobewokens))):
32 tobewoken.time_since_call += 1
33 if tobewoken.time_since_call >= tobewoken.period:
35 tobewoken.time_since_call = 0
36 if tobewoken.one_shot:
38 # delete what we have to delete - in reverse order so the indices stay valid!
41 del self._tobewokens[idx]