module documentation

Utility functions for retrying function calls with a timeout.

Exception RetryAgain Retry again.
Exception RetryTimeout Retry loop timed out.
Function CountRetry A wrapper over SimpleRetry implementing a count down.
Function Retry Call a function repeatedly until it succeeds.
Function RetryByNumberOfTimes Retries calling a function up to the specified number of times.
Function SimpleRetry A wrapper over Retry implementing a simpler interface.
Constant RETRY_REMAINING_TIME Undocumented
Class _RetryDelayCalculator Calculator for increasing delays.
def CountRetry(expected, fn, count, args=None):

A wrapper over SimpleRetry implementing a count down.

Where Retry fixes the time, after which the command is assumed to be failing, this function assumes the total number of tries.

See Also
Retry
def Retry(fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time):

Call a function repeatedly until it succeeds.

The function fn is called repeatedly until it doesn't throw RetryAgain anymore. Between calls a delay, specified by delay, is inserted. After a total of timeout seconds, this function throws RetryTimeout.

delay can be one of the following:

  • callable returning the delay length as a float
  • Tuple of (start, factor, limit)
  • RETRY_REMAINING_TIME to sleep until the timeout expires (this is useful when overriding wait_fn to wait for an external event)
  • A static delay as a number (int or float)
Parameters
fn:callableFunction to be called
delayEither a callable (returning the delay), a tuple of (start, factor, limit) (see _RetryDelayCalculator), RETRY_REMAINING_TIME or a number (int or float)
timeout:floatTotal timeout
argsUndocumented
wait_fn:callableWaiting function
_time_fnUndocumented
Returns
Return value of function
def RetryByNumberOfTimes(max_retries, exception_class, fn, *args, **kwargs):

Retries calling a function up to the specified number of times.

Parameters
max_retries:integerMaximum number of retries.
exception_class:classException class which is used for throwing the final exception.
fn:callableFunction to be called (up to the specified maximum number of retries.
*argsUndocumented
**kwargsUndocumented
def SimpleRetry(expected, fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time):

A wrapper over Retry implementing a simpler interface.

All the parameters are the same as for Retry, except it has one extra argument: expected, which can be either a value (will be compared with the result of the function, or a callable (which will get the result passed and has to return a boolean). If the test is false, we will retry until either the timeout has passed or the tests succeeds. In both cases, the last result from calling the function will be returned.

Note that this function is not expected to raise any retry-related exceptions, always simply returning values. As such, the function is designed to allow easy wrapping of code that doesn't use retry at all (e.g. "if fn(args)" replaced with "if SimpleRetry(True, fn, ...)".

See Also
Retry
RETRY_REMAINING_TIME =

Undocumented

Value
object()