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