-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.py
More file actions
46 lines (34 loc) · 980 Bytes
/
loop.py
File metadata and controls
46 lines (34 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import time
from twisted.internet import reactor, task
from interfaces import ILoop
class Loop(ILoop):
period = 0.1 # Time to start server in s.
def __init__(self):
self.objects = []
self.last_update = None
self.task = None
def add_object(self, obj):
"""
Add object to the pool of objects
:param obj: Updatable object
:type obj: interfaces.IUpdatable
"""
self.objects.append(obj)
def tick(self):
"""
Process update method of all objects
"""
now = time.time()
# Time between ticks in seconds
t = now - self.last_update
for o in self.objects:
o.update(t)
self.last_update = now
def start(self):
"""
Init loop and start reactor
"""
self.last_update = time.time()
self.task = task.LoopingCall(self.tick)
self.task.start(self.period)
reactor.run()