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
- The decorator will attempt to execute the function
- If an exception from the specified
error_listoccurs, 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
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08969d65d8202d723a8b129e49b24eb9e0a9096d3464c6197874df97969ad0b9
|
|
| MD5 |
9140e77e8ca6c98057ef944a3494b447
|
|
| BLAKE2b-256 |
e404c5e90542dd70d66080d48829f980a47746b0f7bdc3f8b73036a7ef6149db
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f3058789c5f9de22cdf9139b90ba2f964757732b2684b62755ab0555a13d878
|
|
| MD5 |
8d32bd3edfecbd88c7d8d91fdea88d8a
|
|
| BLAKE2b-256 |
59f238eb3459dfc286edd6ea8a46dd1c4ac19941c602fa0f135765bb8f8cb6da
|