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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file reattempt-1.0.9.tar.gz.

File metadata

  • Download URL: reattempt-1.0.9.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for reattempt-1.0.9.tar.gz
Algorithm Hash digest
SHA256 93b6043e9926cb132f4f16800d05ad1c85fe17a7fbe19da919a5d9a6e39a2a04
MD5 8337071835c26af65f821c0a4b80b9e5
BLAKE2b-256 97df8c091ae38fb895f117496d749259837186ac59e790e2072d99617b87dfd5

See more details on using hashes here.

File details

Details for the file reattempt-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: reattempt-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for reattempt-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 56d498f6adedd5169ca295f16a96ceeb18c7e8c290b1566b57b2f115ad379126
MD5 9630c66ca2f439b87d9da4c94a4eac05
BLAKE2b-256 f104badf28449198223e834b628d5a431477560f8d6dc6670ff650912eb4b487

See more details on using hashes here.

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