HWRF  trunk@4391
Functions | Variables
produtil.retry Namespace Reference

Contains retry_io() which automates retrying operations. More...

Detailed Description

Contains retry_io() which automates retrying operations.

Functions

def retry_io
 This function automates retrying an unreliable operation several times until it succeeds. More...
 

Variables

list __all__ = ['retry_io']
 Symbols exported by "from produtil.retry import *".
 

Function Documentation

def produtil.retry.retry_io (   max_tries,
  sleep_time,
  operation,
  opargs = [],
  logger = None,
  fail = None,
  failargs = [],
  giveup = None,
  giveupargs = [],
  randsleep = True,
  backoff = 1.3,
  first_warn = 0,
  giveup_quiet = False 
)

This function automates retrying an unreliable operation several times until it succeeds.

This subroutine will retry the operation up to a maximum number of times. If the operation fails too many times, then the last exception thrown by the operation is passed on (raised) to the caller.

Parameters
max_triesMaximum number of times to attempt the operation (mandatory)
sleep_timeTime to sleep between tries
operationA function or callable object that may thrown an Exception
opargsA list containing arguments to the operation
loggerA logging.Logger object to use for logging, or None to disable logging.
failA string to print, or a function to call, when the operation fails but more retries are possible
failargsOptional: a list of arguments to fail, or None to disable
giveupA string to print, or a function to call when the operation fails too many times, causing retry_io to give up. Default: same as fail
giveupargsOptional: a list of arguments to giveup, or None to disable
randsleepSet to True (default) to enable an exponential backoff algorithm, which will increase the sleep time between tries
backoffThe exponent for the exponential backoff algorithm
first_warnThe first failure at which to warn via the logger
giveup_quietIf True, a WARNING-level message is sent to the logger if the operation fails more than max_tries times.
Returns
The return value of the operation.
Note
If fail or giveup are functions, they are passed the contents of failargs (default: opargs) or giveupargs (default: failargs or opargs) with several additional arguments appended. Those arguments are the exception that was caught, the number of attempts so far, the max_tries, the sleep_time, and then a boolean that is true iff the operation is about to be retried.

Definition at line 13 of file retry.py.