Retry Functions¶
The retry functions can be used to do automatic retries. There are three different ways to use them.
htf.retry()
can be called directly. htf.retriable()
is a decorator that can be used to decorate
functions so that they are retried and htf.retrying()
is a context manager that can be used to do
retries within a context block.
If an exception occurs during execution of the action, it is retried until the maximum number of attempts is reached. Exceptions during the last retry are re-raised to inform the caller of its occurence.
The delay between attempts can also be specified. By default there is no delay.
Direct Calls¶
Direct calls are done using htf.retry()
.
import htf
count = 0
def foo(bar):
global count
print("foo() called with", bar)
print("count:", count)
count += 1
if count < 3:
raise ValueError("count is too small")
return "success"
htf.retry(foo, args=("bar",), attempts=3, delay=1.0)
-
htf.
retry
(action, args=(), kwargs={}, attempts=2, delay=None, retry_exceptions=(<type 'exceptions.BaseException'>, ))¶ Retry
action
with a given number of attempts and a given delay.Parameters: - action (callable) – the action to be retried
- args= () (tuple) – the positional arguments passed to
action
- kwargs={} (dict) – the keyword arguments passed to
action
- attempts=2 (int) – the number of attempts
- delay=None (float or int) – the delay between attempts in seconds
- retry_exceptions= (BaseException,) (tuple) – the exceptions to be catched to retry the action
Returns: the return value of
action(*args, **kwargs)
Using the Decorator¶
If a function or method is supposed to always do retries, the decorator htf.retriable()
can be used.
import htf
count = 0
@htf.retriable(attempts=3, delay=1.0)
def foo(bar):
global count
print("foo() called with", bar)
print("count:", count)
count += 1
if count < 3:
raise ValueError("count is too small")
return "success"
foo("bar")
-
htf.
retriable
(attempts=2, delay=None, retry_exceptions=(<type 'exceptions.BaseException'>, ))¶ A decorator factory for retry(). Wrap your function in @retriable(…) to give it retry powers!
Parameters: - attempts=2 (int) – the number of attempts
- delay=None (float or int) – the delay between attempts in seconds
- retry_exceptions= (BaseException,) (tuple) – the exceptions to be catched to retry the action
Returns: a function decorator supporting retries
Using the Context Manager¶
To use retries within a context block use the htf.retrying()
context manager.
import htf
count = 0
def foo(bar):
global count
print("foo() called with", bar)
print("count:", count)
count += 1
if count < 3:
raise ValueError("count is too small")
return "success"
with htf.retrying(foo, attempts=3, delay=1.0) as retrying_foo:
retrying_foo("bar")
-
htf.
retrying
(*args, **kwds)¶ A context manager for wrapping
action
with retry functionality with a given number of attempts and a given delay.Parameters: - action (callable) – the action to be retried
- attempts=2 (int) – the number of attempts
- delay=None (float or int) – the delay between attempts in seconds
- retry_exceptions= (BaseException,) (tuple) – the exceptions to be catched to retry the action
Returns: a context manager supporting retries