paulb@109 | 1 | #!/usr/bin/env python |
paulb@109 | 2 | |
paulb@109 | 3 | """ |
paulb@109 | 4 | A simple example of parallel computation using a map and managed callables. |
paulb@109 | 5 | """ |
paulb@109 | 6 | |
paulb@109 | 7 | import pprocess |
paulb@109 | 8 | import time |
paulb@109 | 9 | |
paulb@109 | 10 | # Array size and a limit on the number of processes. |
paulb@109 | 11 | |
paulb@109 | 12 | N = 10 |
paulb@109 | 13 | limit = 10 |
paulb@109 | 14 | delay = 1 |
paulb@109 | 15 | |
paulb@109 | 16 | # Work function and monitoring class. |
paulb@109 | 17 | |
paulb@109 | 18 | def calculate(i, j): |
paulb@109 | 19 | |
paulb@109 | 20 | """ |
paulb@109 | 21 | A supposedly time-consuming calculation on 'i' and 'j'. |
paulb@109 | 22 | """ |
paulb@109 | 23 | |
paulb@109 | 24 | time.sleep(delay) |
paulb@109 | 25 | return i * N + j |
paulb@109 | 26 | |
paulb@109 | 27 | # Main program. |
paulb@109 | 28 | |
paulb@109 | 29 | if __name__ == "__main__": |
paulb@109 | 30 | |
paulb@109 | 31 | t = time.time() |
paulb@109 | 32 | |
paulb@109 | 33 | # Initialise the results using map with a limit on the number of |
paulb@109 | 34 | # channels/processes. |
paulb@109 | 35 | |
paulb@109 | 36 | results = pprocess.Map(limit=limit) |
paulb@109 | 37 | |
paulb@109 | 38 | # Wrap the calculate function and manage it. |
paulb@109 | 39 | |
paulb@109 | 40 | calc = results.manage(pprocess.MakeParallel(calculate)) |
paulb@109 | 41 | |
paulb@109 | 42 | # Perform the work. |
paulb@109 | 43 | |
paulb@109 | 44 | print "Calculating..." |
paulb@109 | 45 | for i in range(0, N): |
paulb@109 | 46 | for j in range(0, N): |
paulb@109 | 47 | calc(i, j) |
paulb@109 | 48 | |
paulb@109 | 49 | # Show the results. |
paulb@109 | 50 | |
paulb@109 | 51 | for i in range(0, N): |
paulb@109 | 52 | for result in results[i*N:i*N+N]: |
paulb@109 | 53 | print result, |
paulb@109 | 54 | print |
paulb@109 | 55 | |
paulb@109 | 56 | print "Time taken:", time.time() - t |
paulb@109 | 57 | |
paulb@109 | 58 | # vim: tabstop=4 expandtab shiftwidth=4 |