Skip to main content

Yet another retry decorator for Python

Project description

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 library and has no external dependencies.

As such it does not to any logging or take in a logger class but instead allows the function, if made for it, to know which retry nr it is on.
It also allows for custom handlers to be created for retry and exception handling. These can log or do anything user wants them to do and will also recieve the retry_config.

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=5
retry_delay_seconds=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(extra_kwargs={"retry_delay_seconds": 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
  • retry_config: dict
  • *args
  • **kwargs

It can also contain extra parameters that can be submitted as extra_kwargs dict to the decorator.

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_seconds that can be sent as an extra_kwarg to the decorator to modify the nr of seconds it sleeps for.

See examples in the examples folder

Exception handlers

A exception handler triggers on raising exception. Works the same way as retry_handler except is not expected to return anything. The default raise_exception handler just raises the exception. If no exception is raised by a custom exception_handler the decorator will raise the error after the function.

See examples in the examples folder

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-0.1.4.tar.gz (12.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-0.1.4-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yet_another_retry-0.1.4.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.0

File hashes

Hashes for yet_another_retry-0.1.4.tar.gz
Algorithm Hash digest
SHA256 794da6cf5f67b6b14a8c224b77df93621dfda730b3058135edd82f4aa43fa756
MD5 59f9f1ca2363b90c31f5c292ea4a61f7
BLAKE2b-256 dbbfc822da72b6c1473eeda89f6b15d7396b6895c5a8503fa3a1ef828a41e6e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for yet_another_retry-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0082d11210d14d8b65902824841c2f2f503a544cbe47d5e84f61c7dfeeb645b8
MD5 3dacdaae35d201e9b0b23a5ebf825870
BLAKE2b-256 c012c81d17cf72fa5265edc0858a9bda6fe73d637f1709143c647d5fb403be3a

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