Skip to main content

Yet another retry decorator for Python

Project description

Build and Publish PyPI Downloads

yet-another-retry

This package is inspired by other retry-packages.
It takes a slightly different approach to certain things however to allow the decorated function to know it is being retried and take action based on a retry_config.
The package uses only python standard library and has no external dependencies.

It allows for custom handlers to be created for retry and exception handling.
Handlers can log or do anything user wants them to do and will also recieve any input parameters to the retry decorator.

Install

python -m pip install yet-another-retry

then import with:

from yet-another-retry import retry

Usage

Basic usage

This is not much different from other retry packages.
Uses the decorator with all default values
tries = 3
retry_delay = 0

from yet_another_retry import retry

@retry()
def my_function():
  ...

Changing number of tries

Since we have to know in advance how many tries to do, this is a parameter directly to the decorator

from yet_another_retry import retry

@retry(tries=5)
def my_function():
  ...

Changing number of seconds to sleep in default retry handler

The default retry handler has a parameter "retry_delay_seconds" that can be modified as an extra_kwarg.

from yet_another_retry import retry

@retry(retry_delay=10)
def my_function():
  ...

Handlers

The decorator uses a concept of handlers for retries and exceptions.
A handler is just a function with some requirements.
The handler will be called on retries and final exception.
Both types of handlers must have the following parameters:

  • e: Exception - must be first accepted parameter
  • **kwargs - must be last accepted parameter

Keep in mind that any parameters added to the retry decorator will be passed on to both the retry and exception handlers.

Retry handlers

A retry handler is expected to return a float or integer that the retry function will sleep for before the next attempt.
The default retry handler has a parameter retry_delay that can be sent as a parameter to the decorator to modify the number of seconds it sleeps for.

See examples in the examples folder

To use a custom retry handler:

from yet_another_retry import retry

def custom_retry_handler(e: Exception, **kwargs):
    sleep_time = 1
    return sleeptime
    
    
@retry(retry_handler=custom_retry_handler)
def my_function():
  ...

Exception handlers

An exception handler is called if the last try raises and exception. It the same way as retry_handler except it is not expected to return anything. The default exception handler just raises the exception. By default if a custom decorator has not raised the exception the retry decorator will raise it after the exception_handler is done. To stop the decorator from raising the exception, if using a custom exception_handler, you can pass raise_final_exception=False to the decorator.

Keep in mind that any parameters added to the retry decorator will be passed on to both the retry and exception handlers.

See examples in the examples folder

To use a custom exception handler:

from yet_another_retry import retry

def custom_exception_handler(e: Exception, **kwargs):
    ... do things 
    raise e
    
    
@retry(exception_handler=custom_exception_handler)
def my_function():
  ...

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

yet_another_retry-1.0.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yet_another_retry-1.0.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file yet_another_retry-1.0.1.tar.gz.

File metadata

  • Download URL: yet_another_retry-1.0.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yet_another_retry-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3e2304fc8153b8d5f1e69ea534070c9131499801669eed2522a55b9f8241e677
MD5 14445faa21d851bac0bb9ec47b70452e
BLAKE2b-256 1944a841d8740929280cabc81bb181da4013a4f65689ae13e3d51a23e5cb95d8

See more details on using hashes here.

File details

Details for the file yet_another_retry-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: yet_another_retry-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for yet_another_retry-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b0a422bf9b8867b5d3ceb36a830f1083073eb768b9592f0d752a5613746e9f8
MD5 8240efd60858319e7caad406a68f18d5
BLAKE2b-256 c6755605d550c9342cf71a351690077220f03c6b4ba8ea88b0a82ba94fb61746

See more details on using hashes here.

Supported by

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