Re-implement ToBeWoken as class instead of namedtuple (it must be modifiable)
[saartuer.git] / waker.py
1 from libtuer import ThreadRepeater
2 from threading import Lock
3
4 SLEEP_TIME = 0.5
5
6 class ToBeWoken:
7         '''a simple struct storing information about a to-be-woken function'''
8         def __init__(self, f, period, one_shot):
9                 self.f = f
10                 self.period = period
11                 self.time_since_call = 0
12                 self.one_shot = one_shot
13
14 class Waker():
15         def __init__(self, sm):
16                 self._sm = sm
17                 self._t = ThreadRepeater(self._wake, SLEEP_TIME, name="Waker")
18                 self._tobewokens = []
19                 self._tobewokens_lock = Lock()
20         
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))
26         
27         def _wake(self):
28                 with self._tobewokens_lock:
29                         delete = []
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:
34                                         tobewoken.f()
35                                         tobewoken.time_since_call = 0
36                                         if tobewoken.one_shot:
37                                                 delete.append(idx)
38                         # delete what we have to delete - in reverse order so the indices stay valid!
39                         delete.reverse()
40                         for idx in delete:
41                                 del self._tobewokens[idx]
42         
43         def stop(self):
44                 self._t.stop()