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 #import random 10 11 # Array size and a limit on the number of processes. 12 13 N = 10 14 limit = 10 15 delay = 1 16 17 # Work function. 18 19 def calculate(t): 20 21 "A supposedly time-consuming calculation on 't'." 22 23 i, j = t 24 #time.sleep(delay * random.random()) 25 time.sleep(delay) 26 return i * N + j 27 28 # Main program. 29 30 if __name__ == "__main__": 31 32 t = time.time() 33 34 # Initialise an array. 35 36 sequence = [] 37 for i in range(0, N): 38 for j in range(0, N): 39 sequence.append((i, j)) 40 41 # Perform the work. 42 43 results = pprocess.pmap(calculate, sequence, limit=limit) 44 45 # Show the results. 46 47 for i in range(0, N): 48 for result in results[i*N:i*N+N]: 49 print result, 50 print 51 52 print "Time taken:", time.time() - t 53 54 # vim: tabstop=4 expandtab shiftwidth=4