aiohttp-retry

2.9.1 · active · verified Sun Mar 29

aiohttp-retry is a simple retry client for aiohttp, providing robust request retrying capabilities for asynchronous HTTP operations. It supports various retry strategies like exponential backoff and random delays. The current version is 2.9.1 and it requires Python 3.7 or higher. The library is actively maintained with regular releases addressing bug fixes and introducing new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `RetryClient` with `ExponentialRetry` options and make a GET request. The `raise_for_status=False` parameter is often used to ensure that aiohttp-retry's retry logic can intercept and handle HTTP error responses instead of aiohttp immediately raising an exception. The example attempts to hit a 500 status endpoint, which should trigger the retry mechanism.

import asyncio
from aiohttp_retry import RetryClient, ExponentialRetry

async def main():
    # Configure retry options, e.g., 3 attempts with exponential backoff
    retry_options = ExponentialRetry(attempts=3, start_timeout=0.1, factor=2)

    # Create a RetryClient instance
    # raise_for_status=False prevents aiohttp from raising exceptions for HTTP error statuses,
    # allowing aiohttp-retry to handle them based on its retry logic.
    async with RetryClient(raise_for_status=False, retry_options=retry_options) as retry_client:
        try:
            # Make a GET request that will be retried on failure
            async with retry_client.get('https://httpbin.org/status/500', retry_attempts=3) as response:
                print(f"Final status: {response.status}")
                print(f"Content-type: {response.headers.get('Content-Type')}")
        except Exception as e:
            print(f"Request failed after retries: {e}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →