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