1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#!/usr/bin/env python
"""
A simple example of parallel computation using a queue and managed callables.
"""
import pprocess
import time
#import random
# Array size and a limit on the number of processes.
N = 10
limit = 10
delay = 1
# Work function and monitoring class.
def calculate(i, j):
"""
A supposedly time-consuming calculation on 'i' and 'j'.
"""
#time.sleep(delay * random.random())
time.sleep(delay)
return (i, j, i * N + j)
# Main program.
if __name__ == "__main__":
t = time.time()
# Initialise the communications queue with a limit on the number of
# channels/processes.
queue = pprocess.Queue(limit=limit)
# Initialise an array.
results = [0] * N * N
# Wrap the calculate function and manage it.
calc = queue.manage(pprocess.MakeParallel(calculate))
# Perform the work.
print "Calculating..."
for i in range(0, N):
for j in range(0, N):
calc(i, j)
# Store the results as they arrive.
print "Finishing..."
for i, j, result in queue:
results[i*N+j] = result
# Show the results.
for i in range(0, N):
for result in results[i*N:i*N+N]:
print result,
print
print "Time taken:", time.time() - t
# vim: tabstop=4 expandtab shiftwidth=4
|