Popular recipes tagged "parallel", "map" and "threads"http://code.activestate.com/recipes/tags/parallel+map+threads/2008-09-17T17:01:21-07:00ActiveState Code Recipesprocessing.Pool variation which allows multiple threads to send the same requests without incurring duplicate processing (Python)
2008-09-17T17:01:21-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576462-processingpool-variation-which-allows-multiple-thr/
<p style="color: grey">
Python
recipe 576462
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/parallel/">parallel</a>, <a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>processing.Pool (<a href="http://pypi.python.org/pypi/processing" rel="nofollow">http://pypi.python.org/pypi/processing</a>) is a nice tool to "parallelize" map() on multiple CPUs.
However, imagine you have X threads which send the same request Pool.map(getNthPrimeNumber, [100000, 10000000, 10000]) at (almost) the same time. Obviously, you don't want to compute X times getNthPrimeNumber for 100000, 10000000, 10000... unless you have 3.X processors available. You would like one thread to submit the 3 requests, and then the X-1 others would notice that the requests have already been submitted and will then just wait for the result.
This is what this code is about: a kind of "trensient memoize" for processing.Pool::imap().</p>