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.10.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

reattempt-1.0.10-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reattempt-1.0.10.tar.gz
  • Upload date:
  • Size: 12.0 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.10.tar.gz
Algorithm Hash digest
SHA256 56046a7ce9b345f32454371d5459eeb4a81efafbda8cbed3be243a6bd3d9e2c9
MD5 4402399baf55c08ca624c2c289fd673f
BLAKE2b-256 439266078a6d4018d803f85db2a7232cf3469f1e0bc58cb8ea962a17818363b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reattempt-1.0.10-py3-none-any.whl
  • Upload date:
  • Size: 4.5 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.10-py3-none-any.whl
Algorithm Hash digest
SHA256 54962f6db26f9a5929d25e8fbcea522517ef4c36017e03b968abe0957d29258b
MD5 bd3b2b3be2a2a45425c91a4f3f45c759
BLAKE2b-256 e18430d92f5d4f6a1c76c9c67fa97f5a8da1e6c23dd20c72b6da8d10bb8ac173

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