]> jfr.im git - irc/rizon/acid.git/blame - pyva/pyva/src/main/python/task.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / task.py
CommitLineData
685e346e
A
1import time
2import logging
3
4timers = []
5
6def TimerCompare(t1, t2):
7 if t1.tick < t2.tick:
8 return -1
9 elif t1.tick > t2.tick:
10 return 1
11 else:
12 return 0
13
14class Task():
15 _log = logging.getLogger(__name__)
16 secs = 0
17 tick = 0
18 repeating = None
19 func = None
20
21 def start(self, secs, repeating = True):
22 self.secs = secs
23 self.tick = int(time.time()) + secs
24 self.repeating = repeating
25 global timers
26 if not timers.count(self):
27 timers.append(self)
28 timers = sorted(timers, cmp=TimerCompare)
29
30 def stop(self):
31 try:
32 global timers
33 timers.remove(self)
34 except:
35 pass
36
37 def run(self, ts):
38 self._log.debug("Running timer " + str(self.func))
39 self.func()
40
41 if not self.repeating:
42 self.stop()
43 else:
44 global timers
45 self.tick = ts + self.secs
46 timers = sorted(timers, cmp=TimerCompare)
47
48def LoopingCall(func):
49 t = Task()
50 t.func = func
51 return t
52
53def Run():
54 now = int(time.time())
55
56 global timers
57 while len(timers) > 0 and timers[0].tick <= now:
58 timers[0].run(now)