oops, Waker needs locking
[saartuer.git] / waker.py
1 from libtuer import ThreadRepeater
2 from collections import namedtuple
3 from threading import Lock
4
5 SLEEP_TIME = 0.5
6
7 ToBeWoken = namedtuple('ToBeWoken','f period time_since_call one_shot')
8
9 class Waker():
10         def __init__(self, sm):
11                 self._sm = sm
12                 self._t = ThreadRepeater(self._wake, SLEEP_TIME, name="Waker")
13                 self._tobewokens = []
14                 self._tobewokens_lock = Lock()
15         
16         def register(f, time, one_shot = False):
17                 '''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!'''
18                 time = max(time//SLEEP_TIME, 1)
19                 with self._tobewokens_lock:
20                         self._tobewokens.append(ToBeWoken(f, time, 0, one_shot))
21         
22         def _wake(self):
23                 with self._tobewokens_lock:
24                         delete = []
25                         # run the functions we ought to run
26                         for tobewoken, idx in zip(self._tobewokens, range(len(self._tobewokens))):
27                                 tobewoken.time_since_call += 1
28                                 if tobewoken.time_since_call >= tobewoken.period:
29                                         tobewoken.f()
30                                         tobewoken.time_since_call = 0
31                                         if tobewoken.one_shot:
32                                                 delete.append(idx)
33                         # delete what we have to delete - in reverse order so the indices stay valid!
34                         delete.reverse()
35                         for idx in delete:
36                                 del self._tobewokens[idx]
37         
38         def stop(self):
39                 self._t.stop()