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