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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 Args: | 97 Args: |
98 thread: a ReraiserThread object. | 98 thread: a ReraiserThread object. |
99 """ | 99 """ |
100 self._threads.append(thread) | 100 self._threads.append(thread) |
101 | 101 |
102 def StartAll(self): | 102 def StartAll(self): |
103 """Start all threads.""" | 103 """Start all threads.""" |
104 for thread in self._threads: | 104 for thread in self._threads: |
105 thread.start() | 105 thread.start() |
106 | 106 |
107 def _JoinAll(self, watcher=watchdog_timer.WatchdogTimer(None)): | 107 def _JoinAll(self, watcher): |
108 """Join all threads without stack dumps. | 108 """Join all threads without stack dumps. |
109 | 109 |
110 Reraises exceptions raised by the child threads and supports breaking | 110 Reraises exceptions raised by the child threads and supports breaking |
111 immediately on exceptions raised on the main thread. | 111 immediately on exceptions raised on the main thread. |
112 | 112 |
113 Args: | 113 Args: |
114 watcher: Watchdog object providing timeout, by default waits forever. | 114 watcher: Watchdog object providing timeout, by default waits forever. |
115 """ | 115 """ |
116 alive_threads = self._threads[:] | 116 alive_threads = self._threads[:] |
117 while alive_threads: | 117 while alive_threads: |
118 for thread in alive_threads[:]: | 118 for thread in alive_threads[:]: |
119 if watcher.IsTimedOut(): | 119 if watcher.IsTimedOut(): |
120 raise TimeoutError('Timed out waiting for %d of %d threads.' % | 120 raise TimeoutError('Timed out waiting for %d of %d threads.' % |
jbudorick
2014/10/28 17:28:46
One other thing to consider (which you may already
| |
121 (len(alive_threads), len(self._threads))) | 121 (len(alive_threads), len(self._threads))) |
122 # Allow the main thread to periodically check for interrupts. | 122 # Allow the main thread to periodically check for interrupts. |
123 thread.join(0.1) | 123 thread.join(0.1) |
124 if not thread.isAlive(): | 124 if not thread.isAlive(): |
125 alive_threads.remove(thread) | 125 alive_threads.remove(thread) |
126 # All threads are allowed to complete before reraising exceptions. | 126 # All threads are allowed to complete before reraising exceptions. |
127 for thread in self._threads: | 127 for thread in self._threads: |
128 thread.ReraiseIfException() | 128 thread.ReraiseIfException() |
129 | 129 |
130 def JoinAll(self, watcher=watchdog_timer.WatchdogTimer(None)): | 130 def JoinAll(self, watcher=None): |
131 """Join all threads. | 131 """Join all threads. |
132 | 132 |
133 Reraises exceptions raised by the child threads and supports breaking | 133 Reraises exceptions raised by the child threads and supports breaking |
134 immediately on exceptions raised on the main thread. Unfinished threads' | 134 immediately on exceptions raised on the main thread. Unfinished threads' |
135 stacks will be logged on watchdog timeout. | 135 stacks will be logged on watchdog timeout. |
136 | 136 |
137 Args: | 137 Args: |
138 watcher: Watchdog object providing timeout, by default waits forever. | 138 watcher: Watchdog object providing timeout, by default waits forever. |
139 """ | 139 """ |
140 if watcher is None: | |
141 watcher = watchdog_timer.WatchdogTimer(None) | |
140 try: | 142 try: |
141 self._JoinAll(watcher) | 143 self._JoinAll(watcher) |
142 except TimeoutError: | 144 except TimeoutError: |
143 for thread in (t for t in self._threads if t.isAlive()): | 145 for thread in (t for t in self._threads if t.isAlive()): |
144 LogThreadStack(thread) | 146 LogThreadStack(thread) |
145 raise | 147 raise |
146 | 148 |
147 def GetAllReturnValues(self, watcher=watchdog_timer.WatchdogTimer(None)): | 149 def GetAllReturnValues(self, watcher=None): |
148 """Get all return values, joining all threads if necessary. | 150 """Get all return values, joining all threads if necessary. |
149 | 151 |
150 Args: | 152 Args: |
151 watcher: same as in |JoinAll|. Only used if threads are alive. | 153 watcher: same as in |JoinAll|. Only used if threads are alive. |
152 """ | 154 """ |
153 if any([t.isAlive() for t in self._threads]): | 155 if any([t.isAlive() for t in self._threads]): |
154 self.JoinAll(watcher) | 156 self.JoinAll(watcher) |
155 return [t.GetReturnValue() for t in self._threads] | 157 return [t.GetReturnValue() for t in self._threads] |
156 | 158 |
OLD | NEW |