1 #!/usr/bin/env python 2 3 """ 4 A simple example of parallel computation using a queue and the start method. 5 """ 6 7 import pprocess 8 import time 9 10 # Array size and a limit on the number of processes. 11 12 N = 10 13 limit = 10 14 delay = 1 15 16 # Work function. 17 18 def calculate(i, j): 19 20 "A supposedly time-consuming calculation on 'i' and 'j'." 21 22 time.sleep(delay) 23 return (i, j, i * N + j) 24 25 # Main program. 26 27 if __name__ == "__main__": 28 29 t = time.time() 30 31 # Initialise the communications queue with a limit on the number of 32 # channels/processes. 33 34 queue = pprocess.Queue(limit=limit) 35 36 # Initialise an array. 37 38 results = [0] * N * N 39 40 # Wrap the calculate function. 41 42 calc = pprocess.MakeParallel(calculate) 43 44 # Perform the work. 45 46 for i in range(0, N): 47 for j in range(0, N): 48 queue.start(calc, *(i, j)) 49 50 # Store the results as they arrive. 51 52 print "Finishing..." 53 for i, j, result in queue: 54 results[i*N+j] = result 55 56 # Show the results. 57 58 for i in range(0, N): 59 for result in results[i*N:i*N+N]: 60 print result, 61 print 62 63 print "Time taken:", time.time() - t 64 65 # vim: tabstop=4 expandtab shiftwidth=4