Skip to main content

A Python library for handling retries, timeouts and fallbacks in a resilient way.

Project description

# PyResilient

[![PyPI version](https://badge.fury.io/py/PyResilient.svg)](https://pypi.org/project/PyResilient/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

PyResilient is a Python module designed to provide resilience strategies such as retries, timeouts, and fallback mechanisms for your functions. This module is ideal for elegantly and robustly handling errors and exceptions in your applications, enhancing reliability and user experience.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
  - [Fallback Decorator](#fallback-decorator)
  - [Retry Decorator](#retry-decorator)

- [License](#license)
- [Author](#author)
- [Acknowledgments](#acknowledgments)

## Installation

You can install PyResilient using pip:

```sh
pip install PyResilient

PyResilient requires Python 3.8 or higher.

Usage

PyResilient provides decorators that can be easily applied to your functions to add resilience features with minimal code changes.

Fallback Decorator

The fallback decorator allows you to apply a fallback strategy to a function. If the original function raises an exception, the fallback function will be executed.

from PyResilient import fallback

def fallback_function():
    print("Executing fallback function")
    return "Default value"

@fallback(fallback_function, exceptions=(ValueError,))
def my_function():
    raise ValueError("An error occurred")

result = my_function()  # Will print "Executing fallback function"
print(result)  # Outputs: Default value

Parameters:

  • fallback_function (callable): The function to execute if an exception is raised.
  • exceptions (tuple of Exception types, optional): The exceptions that trigger the fallback. Defaults to (Exception,).

Retry Decorator

The retry decorator allows you to retry the execution of a function a specific number of times before failing.

from PyResilient import retry

@retry(retries=3, exceptions=(ValueError,), delay=2)
def my_function():
    print("Attempting to execute function")
    raise ValueError("An error occurred")

my_function()
# Will attempt to execute the function 3 times with a 2-second delay between attempts

Parameters:

  • retries (int): The maximum number of retry attempts.
  • exceptions (tuple of Exception types, optional): The exceptions that trigger a retry. Defaults to (Exception,).
  • delay (int or float, optional): The delay between retry attempts in seconds. Defaults to 0.

Timeout Decorator

The timeout decorator allows you to set a time limit for the execution of a function. If the function does not complete within the specified time, a TimeoutError is raised.

from PyResilient import timeout

@timeout(seconds=5)
def my_function():
    import time
    time.sleep(10)
    return "Completed"

try:
    result = my_function()
except TimeoutError:
    print("Function execution timed out")

Parameters:

  • seconds (int or float): The maximum allowed execution time in seconds.

Note: The timeout decorator uses threading under the hood, which may have implications for thread safety and resource management in your application.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Bastián García

Acknowledgments

  • Inspired by the need for robust error handling mechanisms in Python applications.
  • Thanks to the open-source community for their continuous support and contributions.

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

pyresilient-0.0.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

PyResilient-0.0.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file pyresilient-0.0.3.tar.gz.

File metadata

  • Download URL: pyresilient-0.0.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for pyresilient-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3622ecb1296225929bbf9a626f63137dc1e8c0a71da970cd69d4880867c48558
MD5 e811628fc2e1d402f812d2a496d42e6d
BLAKE2b-256 3c6d09a39ece1c9736c7baaa10b4a71df1d2c2fe0a260d12d4b3db35e3b133c0

See more details on using hashes here.

File details

Details for the file PyResilient-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: PyResilient-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for PyResilient-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5450c64726656fdf83d475ff476be806c1885284c4acf7132eea3bcf0057d4d5
MD5 26ca6d883ff496998b6e241d392cd400
BLAKE2b-256 193f021d293ded71955c919dab9f85db7901f12b4e30c4113d3fb7ff05f3dabf

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