1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple_create_map.py Sat Sep 15 23:48:26 2007 +0000
1.3 @@ -0,0 +1,59 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of parallel computation using a map and the create function.
1.8 +
1.9 +NOTE: We could use the with statement in the innermost loop to package the
1.10 +NOTE: try...finally functionality.
1.11 +"""
1.12 +
1.13 +import pprocess
1.14 +import time
1.15 +#import random
1.16 +
1.17 +# Array size and a limit on the number of processes.
1.18 +
1.19 +N = 10
1.20 +limit = 10
1.21 +delay = 1
1.22 +
1.23 +# Main program.
1.24 +
1.25 +if __name__ == "__main__":
1.26 +
1.27 + t = time.time()
1.28 +
1.29 + # Initialise the results using a map with a limit on the number of
1.30 + # channels/processes.
1.31 +
1.32 + results = pprocess.Map(limit=limit)
1.33 +
1.34 + # Perform the work.
1.35 + # NOTE: Could use the with statement in the loop to package the
1.36 + # NOTE: try...finally functionality.
1.37 +
1.38 + print "Calculating..."
1.39 + for i in range(0, N):
1.40 + for j in range(0, N):
1.41 + ch = results.create()
1.42 + if ch:
1.43 + try: # Calculation work.
1.44 +
1.45 + #time.sleep(delay * random.random())
1.46 + time.sleep(delay)
1.47 + ch.send(i * N + j)
1.48 +
1.49 + finally: # Important finalisation.
1.50 +
1.51 + pprocess.exit(ch)
1.52 +
1.53 + # Show the results.
1.54 +
1.55 + for i in range(0, N):
1.56 + for result in results[i*N:i*N+N]:
1.57 + print result,
1.58 + print
1.59 +
1.60 + print "Time taken:", time.time() - t
1.61 +
1.62 +# vim: tabstop=4 expandtab shiftwidth=4