1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple.py Sun Jan 14 19:11:03 2007 +0000
1.3 @@ -0,0 +1,43 @@
1.4 +import pprocess
1.5 +import time
1.6 +
1.7 +N = 10
1.8 +limit = 10
1.9 +
1.10 +def make_array(n):
1.11 + return [list(x) for x in [(0,) * n] * n]
1.12 +
1.13 +def calculate(ch, i, j):
1.14 + time.sleep(1)
1.15 + ch.send((i, j, i * N + j))
1.16 +
1.17 +def wait(exchange, D):
1.18 + print "Processes:", len(exchange.active())
1.19 + while len(exchange.active()) >= limit:
1.20 + store(exchange, D)
1.21 +
1.22 +def store(exchange, D):
1.23 + for ch in exchange.ready():
1.24 + i, j, result = ch.receive()
1.25 + D[i][j] = result
1.26 +
1.27 +if __name__ == "__main__":
1.28 + D = make_array(N)
1.29 + exchange = pprocess.Exchange()
1.30 +
1.31 + print "Calculating..."
1.32 + for i in range(0, N):
1.33 + for j in range(0, N):
1.34 + ch = pprocess.start(calculate, i, j)
1.35 + wait(exchange, D)
1.36 + exchange.add(ch)
1.37 +
1.38 + print "Finishing..."
1.39 + while exchange.active():
1.40 + store(exchange, D)
1.41 +
1.42 + print
1.43 + for row in D:
1.44 + print row
1.45 +
1.46 +# vim: tabstop=4 expandtab shiftwidth=4