An easy to use, but functional decorator for retrying on exceptions.
pip install reretryreretry
An easy to use retry decorator.
This package is a fork from the retry package, but with some of added community-sourced features.
Features
New features in reretry:
- Log traceback of an error that lead to a failed attempt.
- Call a custom callback after each failed attempt.
- Can be used with async functions.
From original retry:
- Retry on specific exceptions.
- Set a maximum number of retries.
- Set a delay between retries.
- Set a maximum delay between retries.
- Set backoff and jitter parameters.
- Use a custom logger.
- No external dependencies (stdlib only).
- (Optionally) Preserve function signatures (
pip install decorator).
Installation
$ pip install reretry
API
The @retry decorator
Usage
@retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, show_traceback=False, logger=logging_logger, fail_callback=None)
Arguments
-
exceptions: An exception or a tuple of exceptions to catch. Default: Exception. -
tries: The maximum number of attempts. default: -1 (infinite). -
delay: Initial delay between attempts (in seconds). default: 0. -
max_delay: The maximum value of delay (in seconds). default: None (no limit). -
backoff: Multiplier applied to delay between attempts. default: 1 (no backoff). -
: Extra seconds added to delay between attempts. default: 0. Fixed if a number, random if a range tuple (min, max).