栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python错误重试逼疯多少人?解决办法来了

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python错误重试逼疯多少人?解决办法来了

01.安装
pip install tenacity

02.使用 (1)使用规则

● 同一个参数,多个值用 |(或),+(与)进行组合使用

● 不同参数之间,只有组合使用,通过关键字参数传参即可

(2)@retry()【常用】

● 【无条件重试】,只要抛出异常就会重试,直到执行不抛异常

from tenacity import *
一直重试
@retry()
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
一直执行 test_demo 函数
(3)@retry(stop=stop_after_attempt(3))【常用】

● 指定【重试的次数】,如 3 次

from tenacity import *
重试 3 次后停止
@retry(stop=stop_after_attempt(3))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
执行三次 test_demo 函数
(4)@retry(stop=stop_after_delay(5))【常用】

● 指定【重试多长时候后停止】,如 5 秒

from tenacity import *
重试 5 秒后停止
@retry(stop=stop_after_delay(5))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
(5)@retry(stop=(stop_after_delay(1) | stop_after_attempt(5)))

● stop_after_delay()和 stop_after_attempt()组合使用,只要其中一个条件满足,任务就停止

from tenacity import *
重试 2 秒或者重试 50 次停止
@retry(stop=(stop_after_delay(2) | stop_after_attempt(50)))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
(6)@retry(wait=wait_fixed(3))【常用】

● 指定每一次【重试时等待时间】,如 3 秒,每一次重试前都要等待 3 秒钟

from tenacity import *
每次重试的等待时间 5 秒
@retry(wait=wait_fixed(5))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
(7)@retry(wait=wait_random(min=1, max=5))

● 【重试时等待时间】在 min,max 之间随机取值

from tenacity import *

重试间隔时间 1-5 秒随机
@retry(wait=wait_random(min=1, max=5))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
(8)@retry(wait=wait_fixed(3) + wait_random(0, 2))

● wait_fixed(3) 与 wait_random(0, 2)组合使用,两个条件都满足

● 比如每次等待 3 秒

from tenacity import *
随机等待 0-2 秒和每次等待 3 秒重试都满足才会重试
@retry(wait=wait_fixed(3) + wait_random(0, 2))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()
(9)@retry(retry=retry_if_exception_type(IOError))

● 指定特定类型的异常出现时,任务才重试,会一直重试

from tenacity import *
指定 TypeError 才会重复
@retry(retry=retry_if_exception_type(TypeError))
def test_demo():
    print('执行 test_demo')
    print('a'+1)
test_demo()
(10)组合使用
@retry(wait=wait_random(min=1, max=5),stop=stop_after_attempt(3))
def test_demo():
    print('执行 test_demo')
    raise Exception('手动抛出异常')
test_demo()

最后: 可以在公众号:伤心的辣条 ! 免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!喜欢软件测试的小伙伴们,可以加入我们的测试技术交流扣扣群:914172719(里面有各种软件测试资源和技术讨论)


好文推荐

转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!

面试经:一线城市搬砖!又面软件测试岗,5000就知足了…

面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…

什么样的人适合从事软件测试工作?

那个准点下班的人,比我先升职了…

测试岗反复跳槽,跳着跳着就跳没了…

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号