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