Chromium Code Reviews| 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 for d, o in zip(self._orig_objs, self._objs): | |
| 177 # TODO(jbudorick): XXX REMOVE BEFORE COMMITTING | |
|
jbudorick
2014/08/15 17:41:47
This will be gone in the next patchset, fwiw.
| |
| 178 print '%s(%s)' % (f.__name__, d) | |
| 179 r._objs = reraiser_thread.ReraiserThreadGroup( | |
| 180 [reraiser_thread.ReraiserThread( | |
| 181 f, args=tuple([o] + list(args)), kwargs=kwargs, | |
| 182 name='%s(%s)' % (f.__name__, d)) | |
| 183 for d, o in zip(self._orig_objs, self._objs)]) | |
| 184 r._objs.StartAll() # pylint: disable=W0212 | |
| 185 return r | |
| 186 | |
| 158 def _assertNoShadow(self, attr_name): | 187 def _assertNoShadow(self, attr_name): |
| 159 """Ensures that |attr_name| isn't shadowing part of the wrapped obejcts. | 188 """Ensures that |attr_name| isn't shadowing part of the wrapped obejcts. |
| 160 | 189 |
| 161 If the wrapped objects _do_ have an |attr_name| attribute, it will be | 190 If the wrapped objects _do_ have an |attr_name| attribute, it will be |
| 162 inaccessible to clients. | 191 inaccessible to clients. |
| 163 | 192 |
| 164 Args: | 193 Args: |
| 165 attr_name: The attribute to check. | 194 attr_name: The attribute to check. |
| 166 Raises: | 195 Raises: |
| 167 AssertionError if the wrapped objects have an attribute named 'attr_name' | 196 AssertionError if the wrapped objects have an attribute named 'attr_name' |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 187 Returns: | 216 Returns: |
| 188 A Parallelizer emulating the value returned from calling |self| with | 217 A Parallelizer emulating the value returned from calling |self| with |
| 189 |args| and |kwargs|. | 218 |args| and |kwargs|. |
| 190 Raises: | 219 Raises: |
| 191 AttributeError if the wrapped objects aren't callable. | 220 AttributeError if the wrapped objects aren't callable. |
| 192 """ | 221 """ |
| 193 r = super(SyncParallelizer, self).__call__(*args, **kwargs) | 222 r = super(SyncParallelizer, self).__call__(*args, **kwargs) |
| 194 r.pFinish(None) | 223 r.pFinish(None) |
| 195 return r | 224 return r |
| 196 | 225 |
| 226 #override | |
| 227 def pMap(self, f, *args, **kwargs): | |
| 228 """Map a function across the current wrapped objects in parallel. | |
| 229 | |
| 230 This calls f(o, *args, **kwargs) for each o in the set of wrapped objects. | |
| 231 | |
| 232 Note that this call is synchronous. | |
| 233 | |
| 234 Args: | |
| 235 f: The function to call. | |
| 236 args: The positional args to pass to f. | |
| 237 kwargs: The keyword args to pass to f. | |
| 238 Returns: | |
| 239 A Parallelizer wrapping the ReraiserThreadGroup running the map in | |
| 240 parallel. | |
| 241 """ | |
| 242 r = super(SyncParallelizer, self).pMap(f, *args, **kwargs) | |
| 243 r.pFinish(None) | |
| 244 return r | |
| 245 | |
| OLD | NEW |