pprocess

examples/simple_managed_map.py

139:49b5b0a3fa61
2008-05-13 paulb [project @ 2008-05-13 18:05:40 by paulb] Updated release notes.
     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 #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 and monitoring class.    18     19 def calculate(i, j):    20     21     """    22     A supposedly time-consuming calculation on 'i' and 'j'.    23     """    24     25     #time.sleep(delay * random.random())    26     time.sleep(delay)    27     return i * N + j    28     29 # Main program.    30     31 if __name__ == "__main__":    32     33     t = time.time()    34     35     # Initialise the results using a map with a limit on the number of    36     # channels/processes.    37     38     results = pprocess.Map(limit=limit)    39     40     # Wrap the calculate function and manage it.    41     42     calc = results.manage(pprocess.MakeParallel(calculate))    43     44     # Perform the work.    45     46     print "Calculating..."    47     for i in range(0, N):    48         for j in range(0, N):    49             calc(i, j)    50     51     # Show the results.    52     53     for i in range(0, N):    54         for result in results[i*N:i*N+N]:    55             print result,    56         print    57     58     print "Time taken:", time.time() - t    59     60 # vim: tabstop=4 expandtab shiftwidth=4