Skip to main content

A decorator to retry a function if it raises an exception

Project description

piretry

A Python package that provides a simple and flexible retry decorator for both synchronous and asynchronous functions. The decorator allows you to automatically retry function calls when specific exceptions occur.

Features

  • Support for both synchronous and asynchronous functions
  • Configurable retry count
  • Customizable exception handling
  • Detailed retry attempt logging
  • Python 3.12+ support

Installation

You can install the package using pip:

pip install piretry

Usage

Basic Example

from piretry import retry_decorator

# Retry a function 3 times if it raises ValueError or ConnectionError
@retry_decorator(retry_count=3, error_list=[ValueError, ConnectionError])
def my_function():
    # Your code here
    pass

# The function will be retried up to 3 times if it raises the specified exceptions

Async Function Example

from piretry import retry_decorator
import asyncio

@retry_decorator(retry_count=3, error_list=[ValueError, ConnectionError])
async def my_async_function():
    # Your async code here
    pass

# Run the async function
asyncio.run(my_async_function())

Parameters

  • retry_count: Number of retry attempts before giving up (integer)
  • error_list: List of exception classes that should trigger a retry

Behavior

  1. The decorator will attempt to execute the function
  2. If an exception from the specified error_list occurs, it will:
    • Print a message indicating the attempt number and error
    • Retry the function if attempts remain
    • Raise the final exception if max attempts are reached
  3. If the function succeeds, it returns the result immediately

Example with Real Use Case

from piretry import retry_decorator
import requests

@retry_decorator(retry_count=3, error_list=[requests.exceptions.RequestException])
def fetch_data(url):
    response = requests.get(url)
    response.raise_for_status()
    return response.json()

# Usage
try:
    data = fetch_data('https://api.example.com/data')
except requests.exceptions.RequestException as e:
    print(f"Failed to fetch data after 3 attempts: {e}")

Requirements

  • Python >= 3.12
  • setuptools >= 75.8.0

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

piretry-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

piretry-0.1.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file piretry-0.1.1.tar.gz.

File metadata

  • Download URL: piretry-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for piretry-0.1.1.tar.gz
Algorithm Hash digest
SHA256 08969d65d8202d723a8b129e49b24eb9e0a9096d3464c6197874df97969ad0b9
MD5 9140e77e8ca6c98057ef944a3494b447
BLAKE2b-256 e404c5e90542dd70d66080d48829f980a47746b0f7bdc3f8b73036a7ef6149db

See more details on using hashes here.

File details

Details for the file piretry-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: piretry-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for piretry-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f3058789c5f9de22cdf9139b90ba2f964757732b2684b62755ab0555a13d878
MD5 8d32bd3edfecbd88c7d8d91fdea88d8a
BLAKE2b-256 59f238eb3459dfc286edd6ea8a46dd1c4ac19941c602fa0f135765bb8f8cb6da

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