# HG changeset patch # User paulb # Date 1168801863 0 # Node ID 0f1359fd3b783ce85168edb39824d190703eae80 # Parent f77a0e1b1ce9965ef3fbd65707b9af5db989e5b3 [project @ 2007-01-14 19:11:03 by paulb] A simple example showing the basic method for performing a parallel computation using a limited number of processes. diff -r f77a0e1b1ce9 -r 0f1359fd3b78 examples/simple.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple.py Sun Jan 14 19:11:03 2007 +0000 @@ -0,0 +1,43 @@ +import pprocess +import time + +N = 10 +limit = 10 + +def make_array(n): + return [list(x) for x in [(0,) * n] * n] + +def calculate(ch, i, j): + time.sleep(1) + ch.send((i, j, i * N + j)) + +def wait(exchange, D): + print "Processes:", len(exchange.active()) + while len(exchange.active()) >= limit: + store(exchange, D) + +def store(exchange, D): + for ch in exchange.ready(): + i, j, result = ch.receive() + D[i][j] = result + +if __name__ == "__main__": + D = make_array(N) + exchange = pprocess.Exchange() + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + ch = pprocess.start(calculate, i, j) + wait(exchange, D) + exchange.add(ch) + + print "Finishing..." + while exchange.active(): + store(exchange, D) + + print + for row in D: + print row + +# vim: tabstop=4 expandtab shiftwidth=4