1 #!/usr/bin/env python 2 3 """ 4 A simple example of parallel computation using a map 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 * N + j 26 27 # Main program. 28 29 if __name__ == "__main__": 30 31 t = time.time() 32 33 # Initialise the results using map with a limit on the number of 34 # channels/processes. 35 36 results = pprocess.Map(limit=limit) 37 38 # Wrap the calculate function and manage it. 39 40 calc = results.manage(pprocess.MakeParallel(calculate)) 41 42 # Perform the work. 43 44 print "Calculating..." 45 for i in range(0, N): 46 for j in range(0, N): 47 calc(i, j) 48 49 # Show the results. 50 51 for i in range(0, N): 52 for result in results[i*N:i*N+N]: 53 print result, 54 print 55 56 print "Time taken:", time.time() - t 57 58 # vim: tabstop=4 expandtab shiftwidth=4