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 """A utility to run functions with timeouts and retries.""" | 5 """A utility to run functions with timeouts and retries.""" |
6 # pylint: disable=W0702 | 6 # pylint: disable=W0702 |
7 | 7 |
8 import logging | 8 import logging |
9 import threading | 9 import threading |
10 import time | 10 import time |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 current_thread = threading.current_thread() | 78 current_thread = threading.current_thread() |
79 if isinstance(current_thread, TimeoutRetryThread): | 79 if isinstance(current_thread, TimeoutRetryThread): |
80 return current_thread | 80 return current_thread |
81 else: | 81 else: |
82 return None | 82 return None |
83 | 83 |
84 | 84 |
85 def WaitFor(condition, wait_period=5, max_tries=None): | 85 def WaitFor(condition, wait_period=5, max_tries=None): |
86 """Wait for a condition to become true. | 86 """Wait for a condition to become true. |
87 | 87 |
88 Repeadly call the function condition(), with no arguments, until it returns | 88 Repeatedly call the function condition(), with no arguments, until it returns |
89 a true value. | 89 a true value. |
90 | 90 |
91 If called within a TimeoutRetryThread, it cooperates nicely with it. | 91 If called within a TimeoutRetryThread, it cooperates nicely with it. |
92 | 92 |
93 Args: | 93 Args: |
94 condition: function with the condition to check | 94 condition: function with the condition to check |
95 wait_period: number of seconds to wait before retrying to check the | 95 wait_period: number of seconds to wait before retrying to check the |
96 condition | 96 condition |
97 max_tries: maximum number of checks to make, the default tries forever | 97 max_tries: maximum number of checks to make, the default tries forever |
98 or until the TimeoutRetryThread expires. | 98 or until the TimeoutRetryThread expires. |
(...skipping 20 matching lines...) Expand all Loading... |
119 if result: | 119 if result: |
120 return result | 120 return result |
121 if timeout_thread: | 121 if timeout_thread: |
122 # pylint: disable=no-member | 122 # pylint: disable=no-member |
123 timeout_thread.GetRemainingTime(wait_period, | 123 timeout_thread.GetRemainingTime(wait_period, |
124 msg='Timed out waiting for %r' % condition_name) | 124 msg='Timed out waiting for %r' % condition_name) |
125 time.sleep(wait_period) | 125 time.sleep(wait_period) |
126 return None | 126 return None |
127 | 127 |
128 | 128 |
129 def Run(func, timeout, retries, args=None, kwargs=None): | 129 def Run(func, timeout, retries, args=None, kwargs=None, desc=None): |
130 """Runs the passed function in a separate thread with timeouts and retries. | 130 """Runs the passed function in a separate thread with timeouts and retries. |
131 | 131 |
132 Args: | 132 Args: |
133 func: the function to be wrapped. | 133 func: the function to be wrapped. |
134 timeout: the timeout in seconds for each try. | 134 timeout: the timeout in seconds for each try. |
135 retries: the number of retries. | 135 retries: the number of retries. |
136 args: list of positional args to pass to |func|. | 136 args: list of positional args to pass to |func|. |
137 kwargs: dictionary of keyword args to pass to |func|. | 137 kwargs: dictionary of keyword args to pass to |func|. |
| 138 desc: An optional description of |func| used in logging. If omitted, |
| 139 |func.__name__| will be used. |
138 | 140 |
139 Returns: | 141 Returns: |
140 The return value of func(*args, **kwargs). | 142 The return value of func(*args, **kwargs). |
141 """ | 143 """ |
142 if not args: | 144 if not args: |
143 args = [] | 145 args = [] |
144 if not kwargs: | 146 if not kwargs: |
145 kwargs = {} | 147 kwargs = {} |
146 | 148 |
147 # The return value uses a list because Python variables are references, not | 149 # The return value uses a list because Python variables are references, not |
148 # values. Closures make a copy of the reference, so updating the closure's | 150 # values. Closures make a copy of the reference, so updating the closure's |
149 # reference wouldn't update where the original reference pointed. | 151 # reference wouldn't update where the original reference pointed. |
150 ret = [None] | 152 ret = [None] |
151 def RunOnTimeoutThread(): | 153 def RunOnTimeoutThread(): |
152 ret[0] = func(*args, **kwargs) | 154 ret[0] = func(*args, **kwargs) |
153 | 155 |
154 num_try = 1 | 156 num_try = 1 |
155 while True: | 157 while True: |
156 child_thread = TimeoutRetryThread( | 158 child_thread = TimeoutRetryThread( |
157 RunOnTimeoutThread, timeout, | 159 RunOnTimeoutThread, timeout, |
158 name='TimeoutThread-%d-for-%s' % (num_try, | 160 name='TimeoutThread-%d-for-%s' % (num_try, |
159 threading.current_thread().name)) | 161 threading.current_thread().name)) |
160 try: | 162 try: |
161 thread_group = reraiser_thread.ReraiserThreadGroup([child_thread]) | 163 thread_group = reraiser_thread.ReraiserThreadGroup([child_thread]) |
162 thread_group.StartAll() | 164 thread_group.StartAll() |
163 thread_group.JoinAll(child_thread.GetWatcher()) | 165 while True: |
164 return ret[0] | 166 thread_group.JoinAll(watcher=child_thread.GetWatcher(), timeout=60) |
| 167 if thread_group.IsAlive(): |
| 168 logging.info('Still working on %s', desc if desc else func.__name__) |
| 169 else: |
| 170 return ret[0] |
165 except: | 171 except: |
166 child_thread.LogTimeoutException() | 172 child_thread.LogTimeoutException() |
167 if num_try > retries: | 173 if num_try > retries: |
168 raise | 174 raise |
169 num_try += 1 | 175 num_try += 1 |
OLD | NEW |