1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import gc
24
25 import Network
26 import Object
27 from World import World
28 from Timer import Timer
29 from Task import Task
30
32 """Main task scheduler."""
33 - def __init__(self, fps = 60, tickrate = 1.0):
34 self.tasks = []
35 self.frameTasks = []
36 self.timer = Timer(fps = fps, tickrate = tickrate)
37 self.currentTask = None
38 self.paused = []
39 self.running = True
40
42 for t in list(self.tasks + self.frameTasks):
43 self.removeTask(t)
44 self.running = False
45
46 - def addTask(self, task, synchronized = True):
47 """
48 Add a task to the engine.
49
50 @param task: L{Task} to add
51 @type synchronized: bool
52 @param synchronized: If True, the task will be run with small
53 timesteps tied to the engine clock.
54 Otherwise the task will be run once per frame.
55 """
56 if synchronized:
57 queue = self.tasks
58 else:
59 queue = self.frameTasks
60
61 if not task in queue:
62 queue.append(task)
63 task.started()
64
66 """
67 Remove a task from the engine.
68
69 @param task: L{Task} to remove
70 """
71 found = False
72 queues = self._getTaskQueues(task)
73 for q in queues:
74 q.remove(task)
75 if queues:
76 task.stopped()
77
79 queues = []
80 for queue in [self.tasks, self.frameTasks]:
81 if task in queue:
82 queues.append(queue)
83 return queues
84
86 """
87 Pause a task.
88
89 @param task: L{Task} to pause
90 """
91 self.paused.append(task)
92
100
102 """
103 Enable or disable garbage collection whenever a random garbage
104 collection run would be undesirable. Disabling the garbage collector
105 has the unfortunate side-effect that your memory usage will skyrocket.
106 """
107 if enabled:
108 gc.enable()
109 else:
110 gc.disable()
111
113 """
114 Run a garbage collection run.
115 """
116 gc.collect()
117
119 """
120 Increase priority of background threads.
121
122 @param boost True of the scheduling of the main UI thread should be
123 made fairer to background threads, False otherwise.
124 """
125 self.timer.highPriority = not bool(boost)
126
128 if not task in self.paused:
129 self.currentTask = task
130 task.run(ticks)
131 self.currentTask = None
132
134 """Run one cycle of the task scheduler engine."""
135 if not self.frameTasks and not self.tasks:
136 return False
137
138 for task in self.frameTasks:
139 self._runTask(task)
140 for ticks in self.timer.advanceFrame():
141 for task in self.tasks:
142 self._runTask(task, ticks)
143 return True
144