Skip to content

Commit 4298cc3

Browse files
author
Charles Weir
committed
CW: Added Scheduler waitFor coroutine and tests
1 parent bce6da2 commit 4298cc3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

BrickPython/Scheduler.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class StopCoroutineException( Exception ):
1010
ProgramStartTime = datetime.datetime.now()
1111

1212
class Scheduler():
13-
13+
1414
# Public members:
1515
# latestSensorCoroutine - the most recent sensor/control coroutine to have been added.
1616
# latestActionCoroutine - the most recent motor control coroutine to have been added.
17-
17+
1818
# Answers the time in ms since program start.
1919
@staticmethod
2020
def currentTimeMillis():
@@ -27,7 +27,7 @@ def currentTimeMillis():
2727
def nullCoroutine():
2828
while True:
2929
yield
30-
30+
3131
def __init__(self, timeMillisBetweenWorkCalls = 20):
3232
self.timeMillisBetweenWorkCalls = timeMillisBetweenWorkCalls
3333
self.coroutines = []
@@ -49,7 +49,7 @@ def doWork(self):
4949
except Exception as e:
5050
print "Got exception: ", e
5151
self.coroutines.remove( coroutine )
52-
52+
5353
self.updateCoroutine.next()
5454

5555
# Private: Wait time before the next doWork call should be called.
@@ -89,11 +89,11 @@ def stopAllCoroutines(self):
8989
# Number of active coroutines
9090
def numCoroutines( self ):
9191
return len(self.coroutines)
92-
92+
9393
# Answers whether any of the given coroutines are still executing
9494
def stillRunning( self, *coroutineList ):
9595
return any( c in self.coroutines for c in coroutineList )
96-
96+
9797

9898
# Coroutine that executes the given coroutines until the first completes, then stops the others and finishes.
9999
def runTillFirstCompletes( self, *coroutineList ):
@@ -107,7 +107,7 @@ def runTillFirstCompletes( self, *coroutineList ):
107107
print "Got exception: ", e
108108
return
109109
yield
110-
110+
111111
# Coroutine that executes the given coroutines until all have completed.
112112
def runTillAllComplete(self, *coroutineList ):
113113
coroutines = list( coroutineList )
@@ -132,3 +132,7 @@ def doWait( self, timeMillis ):
132132
def withTimeout( self, timeoutMillis, coroutine ):
133133
return self.runTillFirstCompletes( coroutine, self.doWait( timeoutMillis ) )
134134

135+
# Coroutine that waits until the given function (with optional parameters) returns True.
136+
def waitFor(self, function, *args ):
137+
while not function(*args):
138+
yield

test/TestScheduler.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ def testEachCallToACoroutineGetsADifferentTime(self):
231231
for i in range(1,10):
232232
scheduler.doWork()
233233

234+
def testTheWaitForCoroutine(self):
235+
scheduler = Scheduler()
236+
arrayParameter = []
237+
# When we create a WaitFor coroutine with a function that takes one parameter (actually an array)
238+
coroutine = scheduler.waitFor( lambda ap: len(ap) > 0, arrayParameter )
239+
# It runs
240+
for i in range(0,5):
241+
coroutine.next()
242+
# Until the function returns true.
243+
arrayParameter.append(1)
244+
TestScheduler.checkCoroutineFinished( coroutine )
245+
246+
234247
if __name__ == '__main__':
235248
unittest.main()
236249

0 commit comments

Comments
 (0)