1 #!/usr/bin/env python 2 3 """ 4 A simple example of sequential computation using a function, attempting to 5 modify a list/array. 6 """ 7 8 import pprocess 9 import time 10 #import random 11 12 # Array size. 13 14 N = 10 15 limit = 10 16 delay = 1 17 18 # Work function. 19 20 def calculate(results, i, j): 21 22 """ 23 A supposedly time-consuming calculation on 'results' using 'i' and 'j'. 24 """ 25 26 #time.sleep(delay * random.random()) 27 time.sleep(delay) 28 results[i * N + j] *= 2 29 30 # Main program. 31 32 if __name__ == "__main__": 33 34 t = time.time() 35 36 queue = pprocess.Queue(limit=limit) 37 calc = queue.manage(pprocess.MakeParallel(calculate)) 38 39 # Initialise an array. 40 41 results = range(0, 100) 42 43 # Perform the work. 44 45 print "Calculating..." 46 for i in range(0, N): 47 for j in range(0, N): 48 calc(results, i, j) 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