1.1 --- a/docs/tutorial.xhtml Sun Sep 16 19:35:34 2007 +0000
1.2 +++ b/docs/tutorial.xhtml Sun Sep 16 19:36:02 2007 +0000
1.3 @@ -159,7 +159,7 @@
1.4 words - we can make this code use several processes instead of just one. Here
1.5 is the modified code:</p>
1.6
1.7 -<pre>
1.8 +<pre id="simple_managed_map">
1.9 t = time.time()
1.10
1.11 # Initialise the results using a map with a limit on the number of
1.12 @@ -525,6 +525,58 @@
1.13 the results in the same order as the initiation of the computations which
1.14 produced them.</p>
1.15
1.16 +<h2>Reusing Processes in Parallel Programs</h2>
1.17 +
1.18 +<p>One notable aspect of the above programs when parallelised is that each
1.19 +invocation of a computation in parallel creates a new process in which the
1.20 +computation is to be performed, regardless of whether existing processes had
1.21 +just finished producing results and could theoretically have been asked to
1.22 +perform new computations. In other words, processes were created and destroyed
1.23 +instead of being reused.</p>
1.24 +
1.25 +<p>However, we can request that processes be reused for computations by
1.26 +enabling the <code>reuse</code> feature of exchange-like objects and employing
1.27 +suitable reusable callables. Consider this modified version of the <a
1.28 +href="#simple_managed_map">simple_managed_map</a> program:</p>
1.29 +
1.30 +<pre>
1.31 + t = time.time()
1.32 +
1.33 + # Initialise the results using a map with a limit on the number of
1.34 + # channels/processes.
1.35 +
1.36 + results = pprocess.Map(limit=limit<strong>, reuse=1</strong>)
1.37 +
1.38 + # Wrap the calculate function and manage it.
1.39 +
1.40 + calc = results.manage(pprocess.Make<strong>Reusable</strong>(calculate))
1.41 +
1.42 + # Perform the work.
1.43 +
1.44 + print "Calculating..."
1.45 + for i in range(0, N):
1.46 + for j in range(0, N):
1.47 + calc(i, j)
1.48 +
1.49 + # Show the results.
1.50 +
1.51 + for i in range(0, N):
1.52 + for result in results[i*N:i*N+N]:
1.53 + print result,
1.54 + print
1.55 +
1.56 + print "Time taken:", time.time() - t
1.57 +</pre>
1.58 +
1.59 +<p>(This code in context with <code>import</code> statements and functions is
1.60 +found in the <code>examples/simple_manage_map_reusable.py</code> file.)</p>
1.61 +
1.62 +<p>By indicating that processes and channels shall be reused, and by wrapping
1.63 +the <code>calculate</code> function with the necessary support, the
1.64 +computations may be performed in parallel using a pool of processes instead of
1.65 +creating a new process for each computation and then discarding it, only to
1.66 +create a new process for the next computation.</p>
1.67 +
1.68 <h2>Summary</h2>
1.69
1.70 <p>The following table indicates the features used in converting one
1.71 @@ -547,16 +599,16 @@
1.72 <tr>
1.73 <td>simple1</td>
1.74 <td>simple_managed_map</td>
1.75 - <td>Map, manage</td>
1.76 + <td>MakeParallel, Map, manage</td>
1.77 </tr>
1.78 <tr>
1.79 <td rowspan="3">simple2</td>
1.80 <td>simple_managed_queue</td>
1.81 - <td>Queue, manage</td>
1.82 + <td>MakeParallel, Queue, manage</td>
1.83 </tr>
1.84 <tr>
1.85 <td>simple_managed</td>
1.86 - <td>Exchange (subclass), manage, finish</td>
1.87 + <td>MakeParallel, Exchange (subclass), manage, finish</td>
1.88 </tr>
1.89 <tr>
1.90 <td>simple_start</td>