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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 def __getattr__(self, name): | 73 def __getattr__(self, name): |
74 """Emulate getting the |name| attribute of |self|. | 74 """Emulate getting the |name| attribute of |self|. |
75 | 75 |
76 Args: | 76 Args: |
77 name: The name of the attribute to retrieve. | 77 name: The name of the attribute to retrieve. |
78 Returns: | 78 Returns: |
79 A Parallelizer emulating the |name| attribute of |self|. | 79 A Parallelizer emulating the |name| attribute of |self|. |
80 """ | 80 """ |
81 self.pGet(None) | 81 self.pGet(None) |
82 | 82 |
83 r = Parallelizer(self._orig_objs) | 83 r = type(self)(self._orig_objs) |
84 r._objs = [getattr(o, name) for o in self._objs] | 84 r._objs = [getattr(o, name) for o in self._objs] |
85 return r | 85 return r |
86 | 86 |
87 def __getitem__(self, index): | 87 def __getitem__(self, index): |
88 """Emulate getting the value of |self| at |index|. | 88 """Emulate getting the value of |self| at |index|. |
89 | 89 |
90 Returns: | 90 Returns: |
91 A Parallelizer emulating the value of |self| at |index|. | 91 A Parallelizer emulating the value of |self| at |index|. |
92 """ | 92 """ |
93 self.pGet(None) | 93 self.pGet(None) |
94 | 94 |
95 r = Parallelizer(self._orig_objs) | 95 r = type(self)(self._orig_objs) |
96 r._objs = [o[index] for o in self._objs] | 96 r._objs = [o[index] for o in self._objs] |
97 return r | 97 return r |
98 | 98 |
99 def __call__(self, *args, **kwargs): | 99 def __call__(self, *args, **kwargs): |
100 """Emulate calling |self| with |args| and |kwargs|. | 100 """Emulate calling |self| with |args| and |kwargs|. |
101 | 101 |
102 Note that this call is asynchronous. Call pFinish on the return value to | 102 Note that this call is asynchronous. Call pFinish on the return value to |
103 block until the call finishes. | 103 block until the call finishes. |
104 | 104 |
105 Returns: | 105 Returns: |
106 A Parallelizer wrapping the ReraiserThreadGroup running the call in | 106 A Parallelizer wrapping the ReraiserThreadGroup running the call in |
107 parallel. | 107 parallel. |
108 Raises: | 108 Raises: |
109 AttributeError if the wrapped objects aren't callable. | 109 AttributeError if the wrapped objects aren't callable. |
110 """ | 110 """ |
111 self.pGet(None) | 111 self.pGet(None) |
112 | 112 |
113 if not self._objs: | 113 if not self._objs: |
114 raise AttributeError('Nothing to call.') | 114 raise AttributeError('Nothing to call.') |
115 for o in self._objs: | 115 for o in self._objs: |
116 if not callable(o): | 116 if not callable(o): |
117 raise AttributeError("'%s' is not callable" % o.__name__) | 117 raise AttributeError("'%s' is not callable" % o.__name__) |
118 | 118 |
119 r = Parallelizer(self._orig_objs) | 119 r = type(self)(self._orig_objs) |
120 r._objs = reraiser_thread.ReraiserThreadGroup( | 120 r._objs = reraiser_thread.ReraiserThreadGroup( |
121 [reraiser_thread.ReraiserThread( | 121 [reraiser_thread.ReraiserThread( |
122 o, args=args, kwargs=kwargs, | 122 o, args=args, kwargs=kwargs, |
123 name='%s.%s' % (str(d), o.__name__)) | 123 name='%s.%s' % (str(d), o.__name__)) |
124 for d, o in zip(self._orig_objs, self._objs)]) | 124 for d, o in zip(self._orig_objs, self._objs)]) |
125 r._objs.StartAll() # pylint: disable=W0212 | 125 r._objs.StartAll() # pylint: disable=W0212 |
126 return r | 126 return r |
127 | 127 |
128 def pFinish(self, timeout): | 128 def pFinish(self, timeout): |
129 """Finish any outstanding asynchronous operations. | 129 """Finish any outstanding asynchronous operations. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 Returns: | 187 Returns: |
188 A Parallelizer emulating the value returned from calling |self| with | 188 A Parallelizer emulating the value returned from calling |self| with |
189 |args| and |kwargs|. | 189 |args| and |kwargs|. |
190 Raises: | 190 Raises: |
191 AttributeError if the wrapped objects aren't callable. | 191 AttributeError if the wrapped objects aren't callable. |
192 """ | 192 """ |
193 r = super(SyncParallelizer, self).__call__(*args, **kwargs) | 193 r = super(SyncParallelizer, self).__call__(*args, **kwargs) |
194 r.pFinish(None) | 194 r.pFinish(None) |
195 return r | 195 return r |
196 | 196 |
OLD | NEW |