pprocess

examples/simple.py

69:49367c7fd104
2007-01-14 paulb [project @ 2007-01-14 22:53:04 by paulb] Added a simple example of parallel computation.
     1 #!/usr/bin/env python     2      3 """     4 A simple example of parallel computation.     5 """     6      7 import pprocess     8 import time     9     10 # Array size and a limit on the number of processes.    11     12 N = 100    13 limit = 10    14     15 def make_array(n):    16     17     "Make an 'n' * 'n' array initialised with zeros."    18     19     return [list(x) for x in [(0,) * n] * n]    20     21 def calculate(ch, i, j):    22     23     """    24     A time-consuming calculation, using 'ch' to communicate with the parent    25     process, with 'i' and 'j' as operands.    26     """    27     28     time.sleep(1)    29     ch.send((i, j, i * N + j))    30     31 class MyExchange(pprocess.Exchange):    32     33     "Parallel convenience class containing the array assignment operation."    34     35     def store_data(self, ch):    36         i, j, result = ch.receive()    37         self.D[i][j] = result    38     39 # Main program.    40     41 if __name__ == "__main__":    42     43     # Initialise the communications exchange with a limit on the number of    44     # channels/processes.    45     46     exchange = MyExchange(limit=limit)    47     48     # Initialise an array - it is stored in the exchange to permit automatic    49     # assignment of values as the data arrives.    50     51     exchange.D = make_array(N)    52     53     # The parallel computation.    54     55     print "Calculating..."    56     for i in range(0, N):    57         for j in range(0, N):    58             ch = pprocess.start(calculate, i, j)    59             exchange.add_wait(ch)    60     61     print "Finishing..."    62     exchange.finish()    63     64     # Show the result.    65     66     print    67     for row in exchange.D:    68         print row    69     70 # vim: tabstop=4 expandtab shiftwidth=4