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 = 20 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 sequence = [] 33 for i in range(0, N): 34 for j in range(0, N): 35 sequence.append((i, j)) 36 37 results = pprocess.pmap(calculate, sequence, limit=limit) 38 39 print "Time taken:", time.time() - t 40 for i in range(0, N): 41 for result in results[i*N:i*N+N]: 42 print result, 43 print 44 45 # vim: tabstop=4 expandtab shiftwidth=4