pprocess

examples/simple_managed_queue.py

138:e1b43585087b
2008-05-13 paulb [project @ 2008-05-13 18:05:27 by paulb] Added missing stylesheet.
     1 #!/usr/bin/env python     2      3 """     4 A simple example of parallel computation using a queue 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, j, i * N + j)    28     29 # Main program.    30     31 if __name__ == "__main__":    32     33     t = time.time()    34     35     # Initialise the communications queue with a limit on the number of    36     # channels/processes.    37     38     queue = pprocess.Queue(limit=limit)    39     40     # Initialise an array.    41     42     results = [0] * N * N    43     44     # Wrap the calculate function and manage it.    45     46     calc = queue.manage(pprocess.MakeParallel(calculate))    47     48     # Perform the work.    49     50     print "Calculating..."    51     for i in range(0, N):    52         for j in range(0, N):    53             calc(i, j)    54     55     # Store the results as they arrive.    56     57     print "Finishing..."    58     for i, j, result in queue:    59         results[i*N+j] = result    60     61     # Show the results.    62     63     for i in range(0, N):    64         for result in results[i*N:i*N+N]:    65             print result,    66         print    67     68     print "Time taken:", time.time() - t    69     70 # vim: tabstop=4 expandtab shiftwidth=4