1 #!/usr/bin/env python 2 3 """ 4 A simple example of parallel computation using a map and the create function. 5 6 NOTE: We could use the with statement in the innermost loop to package the 7 NOTE: try...finally functionality. 8 """ 9 10 import pprocess 11 import time 12 #import random 13 14 # Array size and a limit on the number of processes. 15 16 N = 10 17 limit = 10 18 delay = 1 19 20 # Main program. 21 22 if __name__ == "__main__": 23 24 t = time.time() 25 26 # Initialise the results using a map with a limit on the number of 27 # channels/processes. 28 29 results = pprocess.Map(limit=limit) 30 31 # Perform the work. 32 # NOTE: Could use the with statement in the loop to package the 33 # NOTE: try...finally functionality. 34 35 print "Calculating..." 36 for i in range(0, N): 37 for j in range(0, N): 38 ch = results.create() 39 if ch: 40 try: # Calculation work. 41 42 #time.sleep(delay * random.random()) 43 time.sleep(delay) 44 ch.send(i * N + j) 45 46 finally: # Important finalisation. 47 48 pprocess.exit(ch) 49 50 # Show the results. 51 52 for i in range(0, N): 53 for result in results[i*N:i*N+N]: 54 print result, 55 print 56 57 print "Time taken:", time.time() - t 58 59 # vim: tabstop=4 expandtab shiftwidth=4