-
Notifications
You must be signed in to change notification settings - Fork 3
/
AsyncServerUpdater.py
62 lines (47 loc) · 2.37 KB
/
AsyncServerUpdater.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Queue
import threading
class AsyncTimedServerVariableUpdater():
def __init__(self, updateFrequency):
self.queuedVariableUpdates = Queue.Queue()
self.variables = {}
self.updateFrequency = updateFrequency
self._callUpdateMethodAfterTime()
def addVariable(self, variableName, sendUpdatedVariableToServerFunction, initialValue = 0):
self.variables[variableName] = [initialValue, sendUpdatedVariableToServerFunction, True]
def queueVariableUpdate(self, variableName, variableUpdateFunction):
self.queuedVariableUpdates.put([variableName, variableUpdateFunction])
def stop(self):
self.updateTimer.cancel()
def _updateVariableValues(self):
if self.queuedVariableUpdates.not_empty:
self._applyQueuedVariableUpdates()
self._applyVariableUpdatesToServer()
self._callUpdateMethodAfterTime()
def _applyQueuedVariableUpdates(self):
while self._variableUpdatesExist():
queuedVariableUpdate = self.queuedVariableUpdates.get()
self._applyUpdateToVariable(queuedVariableUpdate)
self.queuedVariableUpdates.task_done()
def _variableUpdatesExist(self):
return self.queuedVariableUpdates.unfinished_tasks != 0
def _applyUpdateToVariable(self, queuedVariableUpdate):
variableName, updateAndReturnVariableValueFunction = queuedVariableUpdate
variable = self.variables[variableName]
currentValue, _, _ = variable
updatedValue = updateAndReturnVariableValueFunction(currentValue)
_setVariableCurrentValue(variable, updatedValue)
_setVariableNeedsUpdate(variable, True)
def _applyVariableUpdatesToServer(self):
for variableNameKey in self.variables:
variable = self.variables[variableNameKey]
currentValue, updateServerVariableFunction, needsUpdate = variable
if needsUpdate is True:
updateServerVariableFunction(variableNameKey, currentValue)
_setVariableNeedsUpdate(variable, False)
def _callUpdateMethodAfterTime(self):
self.updateTimer = threading.Timer(self.updateFrequency, self._updateVariableValues)
self.updateTimer.start()
def _setVariableNeedsUpdate(variable, needsUpdate):
variable[2] = needsUpdate
def _setVariableCurrentValue(variable, newValue):
variable[0] = newValue