1 import pprocess 2 import time 3 4 N = 10 5 limit = 10 6 7 def make_array(n): 8 return [list(x) for x in [(0,) * n] * n] 9 10 def calculate(ch, i, j): 11 time.sleep(1) 12 ch.send((i, j, i * N + j)) 13 14 def wait(exchange, D): 15 print "Processes:", len(exchange.active()) 16 while len(exchange.active()) >= limit: 17 store(exchange, D) 18 19 def store(exchange, D): 20 for ch in exchange.ready(): 21 i, j, result = ch.receive() 22 D[i][j] = result 23 24 if __name__ == "__main__": 25 D = make_array(N) 26 exchange = pprocess.Exchange() 27 28 print "Calculating..." 29 for i in range(0, N): 30 for j in range(0, N): 31 ch = pprocess.start(calculate, i, j) 32 wait(exchange, D) 33 exchange.add(ch) 34 35 print "Finishing..." 36 while exchange.active(): 37 store(exchange, D) 38 39 print 40 for row in D: 41 print row 42 43 # vim: tabstop=4 expandtab shiftwidth=4