1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple_mutation_queue.py Wed Aug 19 11:31:24 2015 +0200
1.3 @@ -0,0 +1,59 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of sequential computation using a function, attempting to
1.8 +modify a list/array.
1.9 +"""
1.10 +
1.11 +import pprocess
1.12 +import time
1.13 +#import random
1.14 +
1.15 +# Array size.
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(results, i, j):
1.24 +
1.25 + """
1.26 + A supposedly time-consuming calculation on 'results' using 'i' and 'j'.
1.27 + """
1.28 +
1.29 + #time.sleep(delay * random.random())
1.30 + time.sleep(delay)
1.31 + results[i * N + j] *= 2
1.32 +
1.33 +# Main program.
1.34 +
1.35 +if __name__ == "__main__":
1.36 +
1.37 + t = time.time()
1.38 +
1.39 + queue = pprocess.Queue(limit=limit)
1.40 + calc = queue.manage(pprocess.MakeParallel(calculate))
1.41 +
1.42 + # Initialise an array.
1.43 +
1.44 + results = range(0, 100)
1.45 +
1.46 + # Perform the work.
1.47 +
1.48 + print "Calculating..."
1.49 + for i in range(0, N):
1.50 + for j in range(0, N):
1.51 + calc(results, i, j)
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