This module provides function decorators which can be used to wrap a function such that it will be retried until some condition is met. It is meant to be of use when accessing unreliable resources with the potential for intermittent failures i.e. network resources and external APIs. Somewhat more generally, it may also be of use for dynamically polling resources for externally generated content.
Since Kenneth Reitz's requests module has become a defacto standard for synchronous HTTP clients in Python, networking examples below are written using it, but it is in no way required by the backoff module.
The on_exception decorator is used to retry when a specified exception is raised. Here's an example using exponential backoff when any requests exception is raised:
The decorator will also accept a tuple of exceptions for cases where the same backoff behavior is desired for more than one exception type: