OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ Wrapper that allows method execution in parallel. | 5 """ Wrapper that allows method execution in parallel. |
6 | 6 |
7 This class wraps a list of objects of the same type, emulates their | 7 This class wraps a list of objects of the same type, emulates their |
8 interface, and executes any functions called on the objects in parallel | 8 interface, and executes any functions called on the objects in parallel |
9 in ReraiserThreads. | 9 in ReraiserThreads. |
10 | 10 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 timeout: Same as |pFinish|. | 148 timeout: Same as |pFinish|. |
149 Returns: | 149 Returns: |
150 A list of the results, in order of the provided devices. | 150 A list of the results, in order of the provided devices. |
151 Raises: | 151 Raises: |
152 Any exception raised by any of the called functions. | 152 Any exception raised by any of the called functions. |
153 """ | 153 """ |
154 self._assertNoShadow('pGet') | 154 self._assertNoShadow('pGet') |
155 self.pFinish(timeout) | 155 self.pFinish(timeout) |
156 return self._objs | 156 return self._objs |
157 | 157 |
| 158 def pMap(self, f, *args, **kwargs): |
| 159 """Map a function across the current wrapped objects in parallel. |
| 160 |
| 161 This calls f(o, *args, **kwargs) for each o in the set of wrapped objects. |
| 162 |
| 163 Note that this call is asynchronous. Call pFinish on the return value to |
| 164 block until the call finishes. |
| 165 |
| 166 Args: |
| 167 f: The function to call. |
| 168 args: The positional args to pass to f. |
| 169 kwargs: The keyword args to pass to f. |
| 170 Returns: |
| 171 A Parallelizer wrapping the ReraiserThreadGroup running the map in |
| 172 parallel. |
| 173 """ |
| 174 self._assertNoShadow('pMap') |
| 175 r = type(self)(self._orig_objs) |
| 176 r._objs = reraiser_thread.ReraiserThreadGroup( |
| 177 [reraiser_thread.ReraiserThread( |
| 178 f, args=tuple([o] + list(args)), kwargs=kwargs, |
| 179 name='%s(%s)' % (f.__name__, d)) |
| 180 for d, o in zip(self._orig_objs, self._objs)]) |
| 181 r._objs.StartAll() # pylint: disable=W0212 |
| 182 return r |
| 183 |
158 def _assertNoShadow(self, attr_name): | 184 def _assertNoShadow(self, attr_name): |
159 """Ensures that |attr_name| isn't shadowing part of the wrapped obejcts. | 185 """Ensures that |attr_name| isn't shadowing part of the wrapped obejcts. |
160 | 186 |
161 If the wrapped objects _do_ have an |attr_name| attribute, it will be | 187 If the wrapped objects _do_ have an |attr_name| attribute, it will be |
162 inaccessible to clients. | 188 inaccessible to clients. |
163 | 189 |
164 Args: | 190 Args: |
165 attr_name: The attribute to check. | 191 attr_name: The attribute to check. |
166 Raises: | 192 Raises: |
167 AssertionError if the wrapped objects have an attribute named 'attr_name' | 193 AssertionError if the wrapped objects have an attribute named 'attr_name' |
(...skipping 19 matching lines...) Expand all Loading... |
187 Returns: | 213 Returns: |
188 A Parallelizer emulating the value returned from calling |self| with | 214 A Parallelizer emulating the value returned from calling |self| with |
189 |args| and |kwargs|. | 215 |args| and |kwargs|. |
190 Raises: | 216 Raises: |
191 AttributeError if the wrapped objects aren't callable. | 217 AttributeError if the wrapped objects aren't callable. |
192 """ | 218 """ |
193 r = super(SyncParallelizer, self).__call__(*args, **kwargs) | 219 r = super(SyncParallelizer, self).__call__(*args, **kwargs) |
194 r.pFinish(None) | 220 r.pFinish(None) |
195 return r | 221 return r |
196 | 222 |
| 223 #override |
| 224 def pMap(self, f, *args, **kwargs): |
| 225 """Map a function across the current wrapped objects in parallel. |
| 226 |
| 227 This calls f(o, *args, **kwargs) for each o in the set of wrapped objects. |
| 228 |
| 229 Note that this call is synchronous. |
| 230 |
| 231 Args: |
| 232 f: The function to call. |
| 233 args: The positional args to pass to f. |
| 234 kwargs: The keyword args to pass to f. |
| 235 Returns: |
| 236 A Parallelizer wrapping the ReraiserThreadGroup running the map in |
| 237 parallel. |
| 238 """ |
| 239 r = super(SyncParallelizer, self).pMap(f, *args, **kwargs) |
| 240 r.pFinish(None) |
| 241 return r |
| 242 |
OLD | NEW |