Skip to main content

Retrying a python function when exceptions are raised

Project description

ReAttempt

ReAttempt is a python decorator to retry a function when exceptions are raised.

Demonstration:

from reattempt import reattempt

@reattempt(max_retries=5, min_time=0.1, max_time=2)
def wrong_function():
  raise Exception("failure")

if __name__ == "__main__":
    wrong_function()

------------------------------------------------------- live log call -------------------------------------------------------
WARNING  root:__init__.py:167 [RETRY] Attempt 1/5 failed, retrying in 0.17 seconds...
WARNING  root:__init__.py:167 [RETRY] Attempt 2/5 failed, retrying in 0.19 seconds...
WARNING  root:__init__.py:167 [RETRY] Attempt 3/5 failed, retrying in 0.19 seconds...
WARNING  root:__init__.py:167 [RETRY] Attempt 4/5 failed, retrying in 0.19 seconds...
WARNING  root:__init__.py:163 [RETRY] Attempt 5/5 failed, stopping
ERROR    root:__init__.py:177 [RETRY] Max retries reached

Table of Contents

Description

ReAttempt is a Python library that provides a decorator to automatically retry a function when exceptions are raised. It uses an exponential backoff strategy to wait between retries, ensuring that the function has multiple chances to succeed before ultimately failing.

Installation

# Install the dependency
pip install reattempt
uv add reattempt
poetry add reattempt

Usage

from reattempt import reattempt

@reattempt
def hello_world():
  print("Hello World")
  raise Exception("failure")

if __name__ == "__main__":
    hello_world()

License

ReAttempt is released under the MIT License. See the LICENSE file for more details.

Contact

For questions, suggestions, or issues related to ReAttempt, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reattempt-1.0.9.tar.gz (11.7 kB view hashes)

Uploaded Source

Built Distribution

reattempt-1.0.9-py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page