# HG changeset patch # User paulb # Date 1189971375 0 # Node ID 652cff467690f62f007e10885c9d8dc5b464d2aa # Parent 2318878da974e4b4b627c15a3fba56c6c8a096d4 [project @ 2007-09-16 19:36:15 by paulb] Added an example using MakeReusable. diff -r 2318878da974 -r 652cff467690 examples/simple_managed_map_reusable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_managed_map_reusable.py Sun Sep 16 19:36:15 2007 +0000 @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using a map 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 * N + j + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + # Initialise the results using a map with a limit on the number of + # channels/processes. + + results = pprocess.Map(limit=limit, reuse=1) + + # Wrap the calculate function and manage it. + + calc = results.manage(pprocess.MakeReusable(calculate)) + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + calc(i, j) + + # 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