Skip to main content

使用说明 - 异步请求重试机制

简介

本项目提供了一个异步请求重试机制,旨在处理网络请求中的失败情况。通过使用装饰器,可以轻松地为异步函数添加重试逻辑,以确保在请求失败时能够自动重试,直到达到最大重试次数或成功为止。

主要组件

  1. StateManager: 单例模式的状态管理器,用于管理请求的状态。
  2. RequestResult: 封装请求结果及其尝试次数的类。
  3. Retrier: 管理重试逻辑的类,负责执行请求并根据条件决定是否重试。
  4. retry_on_condition: 装饰器,用于包装需要重试的异步函数。

安装依赖

在使用本项目之前,请确保安装了以下依赖库:

pip install httpx loguru

使用示例

1. 定义异步请求函数

首先,定义一个异步函数,该函数将执行实际的网络请求。例如:

import httpx

async def fetch_data(url: str) -> httpx.Response:
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        response.raise_for_status()  # 如果响应状态码不是200,将引发异常
        return response

2. 使用装饰器添加重试逻辑

使用 retry_on_condition 装饰器来包装你的异步请求函数,以添加重试逻辑。例如:

@retry_on_condition(max_attempts=5, retry_delay=2.0)
async def fetch_with_retry(url: str) -> RequestResult[httpx.Response]:
    return await fetch_data(url)

3. 调用函数并处理结果

调用带有重试逻辑的函数,并处理返回的结果:

import asyncio

async def main():
    url = "https://api.example.com/data"
    result = await fetch_with_retry(url)

    if result.result is not None:
        print("请求成功:", result.result)
    else:
        print(f"请求失败,尝试次数: {result.attempts}")

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

参数说明

retry_on_condition 装饰器参数

  • max_attempts: 最大重试次数,默认为30次。

Metadata

Release files for HttpRetry 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for HttpRetry 0.1.0
File Size Uploaded
httpretry-0.1.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for HttpRetry 0.1.0
File Interpreter ABI Platform
httpretry-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / httpretry-0.1.0.tar.gz

Download URL httpretry-0.1.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ca1d057edf0cc69732ec085d09110226f78ee4eb3a4d3737d5a2027b6164907b
BLAKE2b-256 checksum
How to use checksums
f2f4c0464bc0abcf4165521a5545aa539edfb4a5e5918e936b575d9c270d4255
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.10.8 Windows/10

Release files / httpretry-0.1.0-py3-none-any.whl

Download URL httpretry-0.1.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
60f2308feeea01f253cce67eabc558f75a0811a686c7678dece866055f17088d
BLAKE2b-256 checksum
How to use checksums
f85d7c41ff84161ac5bfaf6853e51c22e568addd47c577aa0e03f8784cf987d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.10.8 Windows/10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page