| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Thread and ThreadGroup that reraise exceptions on the main thread.""" | 5 """Thread and ThreadGroup that reraise exceptions on the main thread.""" |
| 6 # pylint: disable=W0212 | 6 # pylint: disable=W0212 |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import sys | 9 import sys |
| 10 import threading | 10 import threading |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 Reraises exceptions raised by the child threads and supports breaking | 135 Reraises exceptions raised by the child threads and supports breaking |
| 136 immediately on exceptions raised on the main thread. Unfinished threads' | 136 immediately on exceptions raised on the main thread. Unfinished threads' |
| 137 stacks will be logged on watchdog timeout. | 137 stacks will be logged on watchdog timeout. |
| 138 | 138 |
| 139 Args: | 139 Args: |
| 140 watcher: Watchdog object providing timeout, by default waits forever. | 140 watcher: Watchdog object providing timeout, by default waits forever. |
| 141 """ | 141 """ |
| 142 try: | 142 try: |
| 143 self._JoinAll(watcher) | 143 self._JoinAll(watcher) |
| 144 except TimeoutError: | 144 except TimeoutError: |
| 145 logging.critical('Timed out. Dumping threads.') |
| 145 for thread in (t for t in self._threads if t.isAlive()): | 146 for thread in (t for t in self._threads if t.isAlive()): |
| 146 LogThreadStack(thread) | 147 LogThreadStack(thread) |
| 147 raise | 148 raise |
| 148 | 149 |
| 149 def GetAllReturnValues(self, watcher=None): | 150 def GetAllReturnValues(self, watcher=None): |
| 150 """Get all return values, joining all threads if necessary. | 151 """Get all return values, joining all threads if necessary. |
| 151 | 152 |
| 152 Args: | 153 Args: |
| 153 watcher: same as in |JoinAll|. Only used if threads are alive. | 154 watcher: same as in |JoinAll|. Only used if threads are alive. |
| 154 """ | 155 """ |
| 155 if any([t.isAlive() for t in self._threads]): | 156 if any([t.isAlive() for t in self._threads]): |
| 156 self.JoinAll(watcher) | 157 self.JoinAll(watcher) |
| 157 return [t.GetReturnValue() for t in self._threads] | 158 return [t.GetReturnValue() for t in self._threads] |
| 158 | 159 |
| 159 | 160 |
| 160 def RunAsync(funcs, watcher=None): | 161 def RunAsync(funcs, watcher=None): |
| 161 """Executes the given functions in parallel and returns their results. | 162 """Executes the given functions in parallel and returns their results. |
| 162 | 163 |
| 163 Args: | 164 Args: |
| 164 funcs: List of functions to perform on their own threads. | 165 funcs: List of functions to perform on their own threads. |
| 165 watcher: Watchdog object providing timeout, by default waits forever. | 166 watcher: Watchdog object providing timeout, by default waits forever. |
| 166 | 167 |
| 167 Returns: | 168 Returns: |
| 168 A list of return values in the order of the given functions. | 169 A list of return values in the order of the given functions. |
| 169 """ | 170 """ |
| 170 thread_group = ReraiserThreadGroup(ReraiserThread(f) for f in funcs) | 171 thread_group = ReraiserThreadGroup(ReraiserThread(f) for f in funcs) |
| 171 thread_group.StartAll() | 172 thread_group.StartAll() |
| 172 return thread_group.GetAllReturnValues(watcher=watcher) | 173 return thread_group.GetAllReturnValues(watcher=watcher) |
| OLD | NEW |