1 #!/usr/bin/env python 2 3 """ 4 A simple example of parallel computation using map-style processing. 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. 17 18 def calculate(t): 19 20 "A supposedly time-consuming calculation on 't'." 21 22 i, j = t 23 time.sleep(delay) 24 return i * N + j 25 26 # Main program. 27 28 if __name__ == "__main__": 29 30 t = time.time() 31 32 # Initialise an array. 33 34 sequence = [] 35 for i in range(0, N): 36 for j in range(0, N): 37 sequence.append((i, j)) 38 39 # Perform the work. 40 41 results = pprocess.pmap(calculate, sequence, limit=limit) 42 43 # Show the results. 44 45 print "Time taken:", time.time() - t 46 for i in range(0, N): 47 for result in results[i*N:i*N+N]: 48 print result, 49 print 50 51 # vim: tabstop=4 expandtab shiftwidth=4