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 # Initialise the communications queue with a limit on the number of 32 # channels/processes. 33 34 queue = pprocess.Queue(limit=limit) 35 t = time.time() 36 37 # Initialise an array. 38 39 results = [0] * N * N 40 41 # Wrap the calculate function and manage it. 42 43 calc = queue.manage(pprocess.MakeParallel(calculate)) 44 45 # Perform the work. 46 47 print "Calculating..." 48 for i in range(0, N): 49 for j in range(0, N): 50 calc(i, j) 51 52 # Store the results as they arrive. 53 54 print "Finishing..." 55 for i, j, result in queue: 56 results[i*N+j] = result 57 58 # Show the results. 59 60 print "Time taken:", time.time() - t 61 for i in range(0, N): 62 for result in results[i*N:i*N+N]: 63 print result, 64 print 65 66 # vim: tabstop=4 expandtab shiftwidth=4