# HG changeset patch # User paulb # Date 1189971362 0 # Node ID 2318878da974e4b4b627c15a3fba56c6c8a096d4 # Parent 9abc49db3422ae09d2fad7ae0a68f83577e57e93 [project @ 2007-09-16 19:36:02 by paulb] Added MakeReusable and reuse documentation. Improved the summary table. diff -r 9abc49db3422 -r 2318878da974 docs/tutorial.xhtml --- a/docs/tutorial.xhtml Sun Sep 16 19:35:34 2007 +0000 +++ b/docs/tutorial.xhtml Sun Sep 16 19:36:02 2007 +0000 @@ -159,7 +159,7 @@ words - we can make this code use several processes instead of just one. Here is the modified code:

-
+
     t = time.time()
 
     # Initialise the results using a map with a limit on the number of
@@ -525,6 +525,58 @@
 the results in the same order as the initiation of the computations which
 produced them.

+

Reusing Processes in Parallel Programs

+ +

One notable aspect of the above programs when parallelised is that each +invocation of a computation in parallel creates a new process in which the +computation is to be performed, regardless of whether existing processes had +just finished producing results and could theoretically have been asked to +perform new computations. In other words, processes were created and destroyed +instead of being reused.

+ +

However, we can request that processes be reused for computations by +enabling the reuse feature of exchange-like objects and employing +suitable reusable callables. Consider this modified version of the simple_managed_map program:

+ +
+    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
+
+ +

(This code in context with import statements and functions is +found in the examples/simple_manage_map_reusable.py file.)

+ +

By indicating that processes and channels shall be reused, and by wrapping +the calculate function with the necessary support, the +computations may be performed in parallel using a pool of processes instead of +creating a new process for each computation and then discarding it, only to +create a new process for the next computation.

+

Summary

The following table indicates the features used in converting one @@ -547,16 +599,16 @@ simple1 simple_managed_map - Map, manage + MakeParallel, Map, manage simple2 simple_managed_queue - Queue, manage + MakeParallel, Queue, manage simple_managed - Exchange (subclass), manage, finish + MakeParallel, Exchange (subclass), manage, finish simple_start