diff --git a/docs/source/parallel/asyncresult.txt b/docs/source/parallel/asyncresult.txt index a479081..ffe3593 100644 --- a/docs/source/parallel/asyncresult.txt +++ b/docs/source/parallel/asyncresult.txt @@ -101,11 +101,45 @@ Map results are iterable! ========================= When an AsyncResult object has multiple results (e.g. the :class:`~AsyncMapResult` -object), you can actually iterate through them, and act on the results as they arrive: +object), you can actually iterate through results themselves, and act on them as they arrive: .. literalinclude:: ../../examples/parallel/itermapresult.py :language: python :lines: 20-67 + +That is to say, if you treat an AsyncMapResult as if it were a list of your actual +results, it should behave as you would expect, with the only difference being +that you can start iterating through the results before they have even been computed. + +This lets you do a dumb version of map/reduce with the builtin Python functions, +and the only difference between doing this locally and doing it remotely in parallel +is using the asynchronous view.map instead of the builtin map. + + +Here is a simple one-line RMS (root-mean-square) implemented with Python's builtin map/reduce. + +.. sourcecode:: ipython + + In [38]: X = np.linspace(0,100) + + In [39]: from math import sqrt + + In [40]: add = lambda a,b: a+b + + In [41]: sq = lambda x: x*x + + In [42]: sqrt(reduce(add, map(sq, X)) / len(X)) + Out[42]: 58.028845747399714 + + In [43]: sqrt(reduce(add, view.map(sq, X)) / len(X)) + Out[43]: 58.028845747399714 + +To break that down: + +1. ``map(sq, X)`` Compute the square of each element in the list (locally, or in parallel) +2. ``reduce(add, sqX) / len(X)`` compute the mean by summing over the list (or AsyncMapResult) + and dividing by the size +3. take the square root of the resulting number .. seealso::