1 #!/usr/bin/env python 2 3 """ 4 A simple example of parallel computation using message exchanges and the create 5 function. 6 7 NOTE: We could use the with statement in the innermost loop to package the 8 NOTE: try...finally functionality. 9 """ 10 11 import pprocess 12 import time 13 #import random 14 15 # Array size and a limit on the number of processes. 16 17 N = 10 18 limit = 10 19 delay = 1 20 21 # Monitoring class. 22 23 class MyExchange(pprocess.Exchange): 24 25 "Parallel convenience class containing the array assignment operation." 26 27 def store_data(self, ch): 28 i, j, result = ch.receive() 29 self.D[i*N+j] = result 30 31 # Main program. 32 33 if __name__ == "__main__": 34 35 t = time.time() 36 37 # Initialise the communications exchange with a limit on the number of 38 # channels/processes. 39 40 exchange = MyExchange(limit=limit) 41 42 # Initialise an array - it is stored in the exchange to permit automatic 43 # assignment of values as the data arrives. 44 45 results = exchange.D = [0] * N * N 46 47 # Perform the work. 48 49 print "Calculating..." 50 for i in range(0, N): 51 for j in range(0, N): 52 ch = exchange.create() 53 if ch: 54 try: # Calculation work. 55 56 #time.sleep(delay * random.random()) 57 time.sleep(delay) 58 ch.send((i, j, i * N + j)) 59 60 finally: # Important finalisation. 61 62 pprocess.exit(ch) 63 64 # Wait for the results. 65 66 print "Finishing..." 67 exchange.finish() 68 69 # Show the results. 70 71 for i in range(0, N): 72 for result in results[i*N:i*N+N]: 73 print result, 74 print 75 76 print "Time taken:", time.time() - t 77 78 # vim: tabstop=4 expandtab shiftwidth=4