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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 the_foo.bar('world') | 45 the_foo.bar('world') |
46 the_foo.baz.myBazMethod | 46 the_foo.baz.myBazMethod |
47 | 47 |
48 DoesSomethingWithFoo(Parallelizer(list_of_foos)) | 48 DoesSomethingWithFoo(Parallelizer(list_of_foos)) |
49 | 49 |
50 Note that this class spins up a thread for each object. Using this class | 50 Note that this class spins up a thread for each object. Using this class |
51 to parallelize operations that are already fast will incur a net performance | 51 to parallelize operations that are already fast will incur a net performance |
52 penalty. | 52 penalty. |
53 | 53 |
54 """ | 54 """ |
55 # pylint: disable=W0613 | 55 # pylint: disable=protected-access |
56 | 56 |
57 from pylib.utils import reraiser_thread | 57 from pylib.utils import reraiser_thread |
58 from pylib.utils import watchdog_timer | 58 from pylib.utils import watchdog_timer |
59 | 59 |
60 _DEFAULT_TIMEOUT = 30 | 60 _DEFAULT_TIMEOUT = 30 |
61 _DEFAULT_RETRIES = 3 | 61 _DEFAULT_RETRIES = 3 |
62 | 62 |
63 | 63 |
64 class Parallelizer(object): | 64 class Parallelizer(object): |
65 """Allows parallel execution of method calls across a group of objects.""" | 65 """Allows parallel execution of method calls across a group of objects.""" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 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 |
188 inaccessible to clients. | 188 inaccessible to clients. |
189 | 189 |
190 Args: | 190 Args: |
191 attr_name: The attribute to check. | 191 attr_name: The attribute to check. |
192 Raises: | 192 Raises: |
193 AssertionError if the wrapped objects have an attribute named 'attr_name' | 193 AssertionError if the wrapped objects have an attribute named 'attr_name' |
194 or '_assertNoShadow'. | 194 or '_assertNoShadow'. |
195 """ | 195 """ |
196 if isinstance(self._objs, reraiser_thread.ReraiserThreadGroup): | 196 if isinstance(self._objs, reraiser_thread.ReraiserThreadGroup): |
197 assert(not hasattr(self._objs, '_assertNoShadow')) | 197 assert not hasattr(self._objs, '_assertNoShadow') |
198 assert(not hasattr(self._objs, 'pGet')) | 198 assert not hasattr(self._objs, attr_name) |
199 else: | 199 else: |
200 assert(not any(hasattr(o, '_assertNoShadow') for o in self._objs)) | 200 assert not any(hasattr(o, '_assertNoShadow') for o in self._objs) |
201 assert(not any(hasattr(o, 'pGet') for o in self._objs)) | 201 assert not any(hasattr(o, attr_name) for o in self._objs) |
202 | 202 |
203 | 203 |
204 class SyncParallelizer(Parallelizer): | 204 class SyncParallelizer(Parallelizer): |
205 """A Parallelizer that blocks on function calls.""" | 205 """A Parallelizer that blocks on function calls.""" |
206 | 206 |
207 #override | 207 #override |
208 def __call__(self, *args, **kwargs): | 208 def __call__(self, *args, **kwargs): |
209 """Emulate calling |self| with |args| and |kwargs|. | 209 """Emulate calling |self| with |args| and |kwargs|. |
210 | 210 |
211 Note that this call is synchronous. | 211 Note that this call is synchronous. |
(...skipping 21 matching lines...) Expand all Loading... |
233 args: The positional args to pass to f. | 233 args: The positional args to pass to f. |
234 kwargs: The keyword args to pass to f. | 234 kwargs: The keyword args to pass to f. |
235 Returns: | 235 Returns: |
236 A Parallelizer wrapping the ReraiserThreadGroup running the map in | 236 A Parallelizer wrapping the ReraiserThreadGroup running the map in |
237 parallel. | 237 parallel. |
238 """ | 238 """ |
239 r = super(SyncParallelizer, self).pMap(f, *args, **kwargs) | 239 r = super(SyncParallelizer, self).pMap(f, *args, **kwargs) |
240 r.pFinish(None) | 240 r.pFinish(None) |
241 return r | 241 return r |
242 | 242 |
OLD | NEW |