tenacity 报错_关于Python错误重试方法总结

前言

Tenacity是一个 Apache 2.0授权的通用重试库,用 Python 编写,用于简化向几乎所有内容添加重试行为的任务。它起源于一个重新尝试的分支,可惜这个分支已经不复存在了。

使用Tenacity可以用来进行测试用例的重跑,爬虫脚本的重跑,以及抢票的失败重抢等等。。。可以使用的场景也是比较多。

使用

首先安装Tenacity

pip install Tenacity

无限重试

第一个重试案例,因为一直是抛出异常错误,所以无限进行重试执行

from tenacity import retry

@retry()

def test_retry():

print('失败重试中')

raise Exception

test_retry()

cb306486e0154e32260148d8919e2b3c.png

成功则停止

我们来优化成功一次后程序则终止,否则继续重试。

from tenacity import retry

import random

@retry()

def test_retry():

if random.randint(0,10) > 1:

print('失败重试中')

raise Exception

else:

print('成功')

test_retry()

cbbdf03d0affaa3466a8d8f0e36fce7a.png

重试次数

毕竟一直重试需要消耗很多资源,所以我们可以设置一些重试的次数,比如在失败多少次后停止重试,不管有没有成功。

from tenacity import retry,stop_after_attempt

import random

@retry(stop=stop_after_attempt(7))

def test_retry():

# if random.randint(0,10) > 1:

print('失败重试中')

raise Exception

# else:

# print('成功')

test_retry()

485ec706f518a48cf10f12ac9ca77c81.png

重试时间

也可以设置执行的时间

from tenacity import retry,stop_after_attempt,stop_after_delay

import random

from time import sleep

@retry(stop=stop_after_delay(3))

def test_retry():

# if random.randint(0,10) > 1:

sleep(1)

print('失败重试中')

raise Exception

# else:

# print('成功')

test_retry()

24524a63cc58926e502edb4ccf05d7c2.png

条件组合

甚至可以使用多个组合条件进行停止,哪个条件先触发则执行哪个

from tenacity import retry,stop_after_attempt,stop_after_delay

import random

from time import sleep

@retry(stop=stop_after_delay(3) | stop_after_attempt(2))

def test_retry():

# if random.randint(0,10) > 1:

sleep(1)

print('失败重试中')

raise Exception

# else:

# print('成功')

test_retry()

7a76a24588d6b8e8066f767e4f2d1147.png

重试间隔

重试之间的间隔时间太短,所以让我们在重试之间等待2秒钟

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed

import random

import time

@retry(wait=wait_fixed(2))

def test_retry():

# if random.randint(0,10) > 1:

print('失败重试中')

print(time.ctime())

raise Exception

# else:

# print('成功')

test_retry()

b68b8f5684239340e9e4872f5ad3b858.png

重试随机间隔

我们还可以设置一些等待时间范围

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed,wait_random

import random

import time

@retry(wait=wait_random(min=1,max=2))

def test_retry():

# if random.randint(0,10) > 1:

print('失败重试中')

print(time.ctime())

raise Exception

# else:

# print('成功')

test_retry()

bc96d84540edf0eae2164f3d1a9a6430.png

重试前日志

在执行之前打印日志

from tenacity import retry,stop_after_attempt,before_log

import logging

import sys

logging.basicConfig(stream=sys.stderr,level=logging.DEBUG)

logger = logging.getLogger(__name__)

@retry(stop=stop_after_attempt(3),before=before_log(logger,logging.DEBUG))

def test_retry():

print('失败重试中')

raise Exception('Fail')

test_retry()

2e96fb7bb2b92597d423139d8a8342af.png

重试后日志

那么相同的,可以在执行失败后打印日志

from tenacity import retry,stop_after_attempt,after_log

import logging

import sys

logging.basicConfig(stream=sys.stderr,level=logging.DEBUG)

logger = logging.getLogger(__name__)

@retry(stop=stop_after_attempt(3),after=after_log(logger,logging.DEBUG))

def test_retry():

print('失败重试中')

raise Exception('Fail')

test_retry()

c7bdbb973fdff94640daa3abc0dc2182.png

基本常用的功能就这些了,如果有需要深入了解的可以访问github地址:https://github.com/jd/tenacity

到此这篇关于关于Python错误重试方法总结的文章就介绍到这了,更多相关Python错误重试方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(pyTbaoself) C:\Users\Admin>pip install --upgrade pip Traceback (most recent call last): File "d:\python\lib\runpy.py", line 193, in _run_module_as_main return _run_code(code, main_globals, None, File "d:\python\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "D:\python\Scripts\pip.exe\__main__.py", line 5, in <module> File "d:\python\lib\site-packages\pip\_internal\cli\main.py", line 10, in <module> from pip._internal.cli.autocompletion import autocomplete File "d:\python\lib\site-packages\pip\_internal\cli\autocompletion.py", line 10, in <module> from pip._internal.cli.main_parser import create_main_parser File "d:\python\lib\site-packages\pip\_internal\cli\main_parser.py", line 9, in <module> from pip._internal.build_env import get_runnable_pip File "d:\python\lib\site-packages\pip\_internal\build_env.py", line 19, in <module> from pip._internal.cli.spinners import open_spinner File "d:\python\lib\site-packages\pip\_internal\cli\spinners.py", line 9, in <module> from pip._internal.utils.logging import get_indentation File "d:\python\lib\site-packages\pip\_internal\utils\logging.py", line 29, in <module> from pip._internal.utils.misc import ensure_dir File "d:\python\lib\site-packages\pip\_internal\utils\misc.py", line 40, in <module> from pip._vendor.tenacity import retry, stop_after_delay, wait_fixed File "d:\python\lib\site-packages\pip\_vendor\tenacity\__init__.py", line 397, in <module> FutureGenericT = futures.Future[t.Any] TypeError: 'type' object is not subscriptable (pyTbaoself) C:\Users\Admin>
05-26

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值