Tenacity is a powerful library for implementing retries.

Tenacity is a library for implementing automatic retries when errors occur. It allows you to easily add retry logic to any function or block of code, with flexible configuration of conditions and delay times between attempts.

• Tenacity is an excellent tool for handling unstable operations, such as API requests or database connections.

from tenacity import retry, stop_after_attempt, wait_fixed

@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def unreliable_function():
    print("Attempt...")
    raise Exception("Error!")

try:
    unreliable_function()
except Exception as e:
    print(f"All attempts exhausted: {e}")

🔗 Here's the link to the documentation: Tenacity Docs

Comments (0)

Leave a comment