pprocess

examples/simple_create_queue.py

144:c9753c628f21
2008-06-02 paulb [project @ 2008-06-02 22:43:11 by paulb] Updated the release information. Fixed details of the tutorial; added details of the reference document. Changed persistent examples.
     1 #!/usr/bin/env python     2      3 """     4 A simple example of parallel computation using message queues and the create     5 function.     6      7 NOTE: We could use the with statement in the innermost loop to package the     8 NOTE: try...finally functionality.     9 """    10     11 import pprocess    12 import time    13 #import random    14     15 # Array size and a limit on the number of processes.    16     17 N = 10    18 limit = 10    19 delay = 1    20     21 # Main program.    22     23 if __name__ == "__main__":    24     25     t = time.time()    26     27     # Initialise the communications queue with a limit on the number of    28     # channels/processes.    29     30     queue = pprocess.Queue(limit=limit)    31     32     # Initialise an array.    33     34     results = [0] * N * N    35     36     # Perform the work.    37     # NOTE: Could use the with statement in the loop to package the    38     # NOTE: try...finally functionality.    39     40     print "Calculating..."    41     for i in range(0, N):    42         for j in range(0, N):    43             ch = queue.create()    44             if ch:    45                 try: # Calculation work.    46     47                     #time.sleep(delay * random.random())    48                     time.sleep(delay)    49                     ch.send((i, j, i * N + j))    50     51                 finally: # Important finalisation.    52     53                     pprocess.exit(ch)    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