paulb@106 | 1 | #!/usr/bin/env python |
paulb@106 | 2 | |
paulb@106 | 3 | """ |
paulb@106 | 4 | A simple example of parallel computation using message queues and the create |
paulb@106 | 5 | function. |
paulb@106 | 6 | |
paulb@106 | 7 | NOTE: We could use the with statement in the innermost loop to package the |
paulb@106 | 8 | NOTE: try...finally functionality. |
paulb@106 | 9 | """ |
paulb@106 | 10 | |
paulb@106 | 11 | import pprocess |
paulb@106 | 12 | import time |
paulb@112 | 13 | #import random |
paulb@106 | 14 | |
paulb@106 | 15 | # Array size and a limit on the number of processes. |
paulb@106 | 16 | |
paulb@106 | 17 | N = 10 |
paulb@106 | 18 | limit = 10 |
paulb@106 | 19 | delay = 1 |
paulb@106 | 20 | |
paulb@106 | 21 | # Main program. |
paulb@106 | 22 | |
paulb@106 | 23 | if __name__ == "__main__": |
paulb@106 | 24 | |
paulb@109 | 25 | t = time.time() |
paulb@109 | 26 | |
paulb@106 | 27 | # Initialise the communications queue with a limit on the number of |
paulb@106 | 28 | # channels/processes. |
paulb@106 | 29 | |
paulb@106 | 30 | queue = pprocess.Queue(limit=limit) |
paulb@106 | 31 | |
paulb@106 | 32 | # Initialise an array. |
paulb@106 | 33 | |
paulb@109 | 34 | results = [0] * N * N |
paulb@106 | 35 | |
paulb@106 | 36 | # Perform the work. |
paulb@106 | 37 | # NOTE: Could use the with statement in the loop to package the |
paulb@106 | 38 | # NOTE: try...finally functionality. |
paulb@106 | 39 | |
paulb@106 | 40 | print "Calculating..." |
paulb@106 | 41 | for i in range(0, N): |
paulb@106 | 42 | for j in range(0, N): |
paulb@106 | 43 | ch = queue.create() |
paulb@106 | 44 | if ch: |
paulb@106 | 45 | try: # Calculation work. |
paulb@106 | 46 | |
paulb@112 | 47 | #time.sleep(delay * random.random()) |
paulb@106 | 48 | time.sleep(delay) |
paulb@106 | 49 | ch.send((i, j, i * N + j)) |
paulb@106 | 50 | |
paulb@106 | 51 | finally: # Important finalisation. |
paulb@106 | 52 | |
paulb@106 | 53 | pprocess.exit(ch) |
paulb@106 | 54 | |
paulb@106 | 55 | # Store the results as they arrive. |
paulb@106 | 56 | |
paulb@106 | 57 | print "Finishing..." |
paulb@106 | 58 | for i, j, result in queue: |
paulb@109 | 59 | results[i*N+j] = result |
paulb@106 | 60 | |
paulb@106 | 61 | # Show the results. |
paulb@106 | 62 | |
paulb@106 | 63 | for i in range(0, N): |
paulb@109 | 64 | for result in results[i*N:i*N+N]: |
paulb@106 | 65 | print result, |
paulb@106 | 66 | print |
paulb@106 | 67 | |
paulb@109 | 68 | print "Time taken:", time.time() - t |
paulb@109 | 69 | |
paulb@106 | 70 | # vim: tabstop=4 expandtab shiftwidth=4 |