Python3:在访问不可靠服务时的重试策略(持续更新ing...)

诸神缄默不语-个人CSDN博文目录

比较经典的不可靠服务就是访问网络。
最简单的重试策略就是随机time.sleep然后try-catch,这个太基础了我就不写了。本文主要介绍一些比较复杂的策略及其解决方案,比如指数退避及在其基础上衍生出的需求。

1. 基于tenacity库的解决方案

官方GitHub项目:jd/tenacity: Retrying library for Python
官方文档:Tenacity — Tenacity documentation

安装方式:pip install tenacity

1.1 基础的指数退避策略

直接用@retry装饰访问不可靠服务的函数就可以。

代码示例:

...
from tenacity import (
    retry,
    stop_after_attempt,
    wait_random_exponential,
)
...
@retry(wait=wait_random_exponential(min=60, max=6000), stop=stop_after_attempt(6))  #60和6000都是秒数,OpenAI官方给的是1和60,是为了OpenAI的API接口速率限制,跟我的场景不一样。这个根据实际情况确定就行
def avoid_proxy_die(response_data:str):
    response1=requests.post(url='https://api.openai.com/v1/chat/completions',
                        headers={'Authorization':f"Bearer {API_KEY}",
                                 'content-type':'application/json'},
                        data=response_data,
                        proxies={"http":"127.0.0.1:7890","https":"127.0.0.1:7890"}
                        )
    return response1

1.2 对于不同的异常,想要使用不同退避策略的解决方案

比较简单的实现方式就是在特定异常上直接增加指定睡眠时间:

@retry(wait=wait_random_exponential(min=20, max=600),stop=stop_after_attempt(6))
def avoid_ChatGPT_die(response_data:str):
    try:
        response1=requests.post(url='https://api.openai.com/v1/chat/completions',
                        headers={'Authorization':f"Bearer {API_KEY}",
                                 'content-type':'application/json'},
                        data=response_data,
                        proxies={"http":"127.0.0.1:7890","https":"127.0.0.1:7890"}
                        )
    except Exception as e:  #这个是代理寄了
        time.sleep(60)
        raise e
    if response1.status_code==200:
        return response1
    else:  #这些情况包括:超过频率限制,秘钥余额用完了,服务器炸了
        raise Exception

ChatGPT给了一个更复杂的解决方案,我没有实现过,也列出来以供参考。
大致逻辑是给函数再包一层用@retry装饰的函数,对不同的异常分别手动调用不同的退避策略:

from tenacity import retry, stop_after_attempt, wait_random_exponential, RetryError
from time import sleep

class SpecifiedException(Exception):
    pass

class OtherException(Exception):
    pass

@retry(stop=stop_after_attempt(3))
def custom_retry(func):
    try:
        func()
    except SpecifiedException:
        sleep(wait_random_exponential(min=1, max=60).next())
        raise RetryError
    except OtherException:
        sleep(wait_random_exponential(min=60, max=6000).next())
        raise RetryError
    except Exception:
        # handle all other exceptions
        pass

def my_func():
    # your code here
    pass

custom_retry(my_func)

2. backoff库

pypi网站:https://pypi.org/project/backoff/
略,待补。

3. 本文撰写过程中参考的网络资料

  1. Rate Limits - OpenAI API:这个是OpenAI官方提供的参考资料,我主要只看了其中tenacity库相关的内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值