pprocess

examples/simple_start_queue.py

155:970c7d8a5b1b
2009-05-17 Paul Boddie Fixed the get_number_of_cores function to work with /proc/cpuinfo where the "physical id" field is missing. Updated release information.
     1 #!/usr/bin/env python     2      3 """     4 A simple example of parallel computation using a queue and the start method.     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(i, j):    20     21     "A supposedly time-consuming calculation on 'i' and 'j'."    22     23     #time.sleep(delay * random.random())    24     time.sleep(delay)    25     return (i, j, i * N + j)    26     27 # Main program.    28     29 if __name__ == "__main__":    30     31     t = time.time()    32     33     # Initialise the communications queue with a limit on the number of    34     # channels/processes.    35     36     queue = pprocess.Queue(limit=limit)    37     38     # Initialise an array.    39     40     results = [0] * N * N    41     42     # Wrap the calculate function.    43     44     calc = pprocess.MakeParallel(calculate)    45     46     # Perform the work.    47     48     for i in range(0, N):    49         for j in range(0, N):    50             queue.start(calc, *(i, j))    51     52     # Store the results as they arrive.    53     54     print "Finishing..."    55     for i, j, result in queue:    56         results[i*N+j] = result    57     58     # Show the results.    59     60     for i in range(0, N):    61         for result in results[i*N:i*N+N]:    62             print result,    63         print    64     65     print "Time taken:", time.time() - t    66     67 # vim: tabstop=4 expandtab shiftwidth=4