Revision c7d3a832 lib/utils/retry.py
b/lib/utils/retry.py | ||
---|---|---|
182 | 182 |
current_delay = calc_delay() |
183 | 183 |
if current_delay > 0.0: |
184 | 184 |
wait_fn(current_delay) |
185 |
|
|
186 |
|
|
187 |
def SimpleRetry(expected, fn, delay, timeout, args=None, wait_fn=time.sleep, |
|
188 |
_time_fn=time.time): |
|
189 |
"""A wrapper over L{Retry} implementing a simpler interface. |
|
190 |
|
|
191 |
All the parameters are the same as for L{Retry}, except it has one |
|
192 |
extra argument: expected, which can be either a value (will be |
|
193 |
compared with the result of the function, or a callable (which will |
|
194 |
get the result passed and has to return a boolean). If the test is |
|
195 |
false, we will retry until either the timeout has passed or the |
|
196 |
tests succeeds. In both cases, the last result from calling the |
|
197 |
function will be returned. |
|
198 |
|
|
199 |
Note that this function is not expected to raise any retry-related |
|
200 |
exceptions, always simply returning values. As such, the function is |
|
201 |
designed to allow easy wrapping of code that doesn't use retry at |
|
202 |
all (e.g. "if fn(args)" replaced with "if SimpleRetry(True, fn, |
|
203 |
...)". |
|
204 |
|
|
205 |
@see: L{Retry} |
|
206 |
|
|
207 |
""" |
|
208 |
rdict = {} |
|
209 |
def helper(*innerargs): |
|
210 |
# pylint: disable-msg=W0142 |
|
211 |
result = rdict["result"] = fn(*innerargs) |
|
212 |
if not ((callable(expected) and expected(result)) or result == expected): |
|
213 |
raise RetryAgain() |
|
214 |
return result |
|
215 |
|
|
216 |
try: |
|
217 |
result = Retry(helper, delay, timeout, args=args, |
|
218 |
wait_fn=wait_fn, _time_fn=_time_fn) |
|
219 |
except RetryTimeout: |
|
220 |
assert "result" in rdict |
|
221 |
result = rdict["result"] |
|
222 |
return result |
Also available in: Unified diff