1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple_start_queue.py Sat Sep 15 15:57:34 2007 +0000
1.3 @@ -0,0 +1,63 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of parallel computation using a queue and the start method.
1.8 +"""
1.9 +
1.10 +import pprocess
1.11 +import time
1.12 +
1.13 +# Array size and a limit on the number of processes.
1.14 +
1.15 +N = 10
1.16 +limit = 10
1.17 +delay = 1
1.18 +
1.19 +# Work function.
1.20 +
1.21 +def calculate(i, j):
1.22 +
1.23 + "A supposedly time-consuming calculation on 'i' and 'j'."
1.24 +
1.25 + time.sleep(delay)
1.26 + return (i, j, i * N + j)
1.27 +
1.28 +# Main program.
1.29 +
1.30 +if __name__ == "__main__":
1.31 +
1.32 + # Initialise the communications queue with a limit on the number of
1.33 + # channels/processes.
1.34 +
1.35 + queue = pprocess.Queue(limit=limit)
1.36 + t = time.time()
1.37 +
1.38 + # Initialise an array.
1.39 +
1.40 + results = [0] * N * N
1.41 +
1.42 + # Wrap the calculate function.
1.43 +
1.44 + calc = pprocess.MakeParallel(calculate)
1.45 +
1.46 + # Perform the work.
1.47 +
1.48 + for i in range(0, N):
1.49 + for j in range(0, N):
1.50 + queue.start(calc, *(i, j))
1.51 +
1.52 + # Store the results as they arrive.
1.53 +
1.54 + print "Finishing..."
1.55 + for i, j, result in queue:
1.56 + results[i*N+j] = result
1.57 +
1.58 + # Show the results.
1.59 +
1.60 + print "Time taken:", time.time() - t
1.61 + for i in range(0, N):
1.62 + for result in results[i*N:i*N+N]:
1.63 + print result,
1.64 + print
1.65 +
1.66 +# vim: tabstop=4 expandtab shiftwidth=4