Show More
@@ -101,11 +101,45 b' Map results are iterable!' | |||
|
101 | 101 | ========================= |
|
102 | 102 | |
|
103 | 103 | When an AsyncResult object has multiple results (e.g. the :class:`~AsyncMapResult` |
|
104 |
object), you can actually iterate through them, and act on the |
|
|
104 | object), you can actually iterate through results themselves, and act on them as they arrive: | |
|
105 | 105 | |
|
106 | 106 | .. literalinclude:: ../../examples/parallel/itermapresult.py |
|
107 | 107 | :language: python |
|
108 | 108 | :lines: 20-67 |
|
109 | ||
|
110 | That is to say, if you treat an AsyncMapResult as if it were a list of your actual | |
|
111 | results, it should behave as you would expect, with the only difference being | |
|
112 | that you can start iterating through the results before they have even been computed. | |
|
113 | ||
|
114 | This lets you do a dumb version of map/reduce with the builtin Python functions, | |
|
115 | and the only difference between doing this locally and doing it remotely in parallel | |
|
116 | is using the asynchronous view.map instead of the builtin map. | |
|
117 | ||
|
118 | ||
|
119 | Here is a simple one-line RMS (root-mean-square) implemented with Python's builtin map/reduce. | |
|
120 | ||
|
121 | .. sourcecode:: ipython | |
|
122 | ||
|
123 | In [38]: X = np.linspace(0,100) | |
|
124 | ||
|
125 | In [39]: from math import sqrt | |
|
126 | ||
|
127 | In [40]: add = lambda a,b: a+b | |
|
128 | ||
|
129 | In [41]: sq = lambda x: x*x | |
|
130 | ||
|
131 | In [42]: sqrt(reduce(add, map(sq, X)) / len(X)) | |
|
132 | Out[42]: 58.028845747399714 | |
|
133 | ||
|
134 | In [43]: sqrt(reduce(add, view.map(sq, X)) / len(X)) | |
|
135 | Out[43]: 58.028845747399714 | |
|
136 | ||
|
137 | To break that down: | |
|
138 | ||
|
139 | 1. ``map(sq, X)`` Compute the square of each element in the list (locally, or in parallel) | |
|
140 | 2. ``reduce(add, sqX) / len(X)`` compute the mean by summing over the list (or AsyncMapResult) | |
|
141 | and dividing by the size | |
|
142 | 3. take the square root of the resulting number | |
|
109 | 143 | |
|
110 | 144 | .. seealso:: |
|
111 | 145 |
General Comments 0
You need to be logged in to leave comments.
Login now