关于Python错误重试方法总结


Posted in Python onJanuary 03, 2021

前言

Tenacity是一个 Apache 2.0授权的通用重试库,用 Python 编写,用于简化向几乎所有内容添加重试行为的任务。它起源于一个重新尝试的分支,可惜这个分支已经不复存在了。
使用Tenacity可以用来进行测试用例的重跑,爬虫脚本的重跑,以及抢票的失败重抢等等。。。可以使用的场景也是比较多。

使用

首先安装Tenacity

pip install Tenacity

无限重试

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

from tenacity import retry

@retry()
def test_retry():
	print('失败重试中')
 raise Exception
 
test_retry()

关于Python错误重试方法总结

成功则停止

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

from tenacity import retry
import random

@retry()
def test_retry():
 if random.randint(0,10) > 1:
  print('失败重试中')
  raise Exception
 else:
  print('成功')

test_retry()

关于Python错误重试方法总结

重试次数

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

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()

关于Python错误重试方法总结

重试时间

也可以设置执行的时间

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()

关于Python错误重试方法总结

条件组合

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

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()

关于Python错误重试方法总结

重试间隔

重试之间的间隔时间太短,所以让我们在重试之间等待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()

关于Python错误重试方法总结

重试随机间隔

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

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()

关于Python错误重试方法总结

重试前日志

在执行之前打印日志

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()

关于Python错误重试方法总结

重试后日志

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

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()

关于Python错误重试方法总结

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

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

Python 相关文章推荐
python的id()函数介绍
Feb 10 Python
python连接oracle数据库实例
Oct 17 Python
Python学习笔记之os模块使用总结
Nov 03 Python
Python字符串转换成浮点数函数分享
Jul 24 Python
简单谈谈Python中的反转字符串问题
Oct 24 Python
Python中用字符串调用函数或方法示例代码
Aug 04 Python
python中for用来遍历range函数的方法
Jun 08 Python
python绘制直线的方法
Jun 30 Python
Django框架验证码用法实例分析
May 10 Python
Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的
Apr 20 Python
pycharm如何使用anaconda中的各种包(操作步骤)
Jul 31 Python
浅谈Python中的正则表达式
Jun 28 Python
详解python中的异常和文件读写
Jan 03 #Python
python绘制雷达图实例讲解
Jan 03 #Python
python 使用xlsxwriter循环向excel中插入数据和图片的操作
Jan 01 #Python
python安装mysql的依赖包mysql-python操作
Jan 01 #Python
python UDF 实现对csv批量md5加密操作
Jan 01 #Python
安装python依赖包psycopg2来调用postgresql的操作
Jan 01 #Python
python matlab库简单用法讲解
Dec 31 #Python
You might like
外媒评选出10支2020年最受欢迎的Dota2战队
2021/03/05 DOTA
php下统计用户在线时间的一种尝试
2010/08/26 PHP
JS 网站性能优化笔记
2011/05/24 PHP
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
php 获取SWF动画截图示例代码
2014/02/10 PHP
php实现MySQL数据库备份与还原类实例
2014/12/09 PHP
js滚动条回到顶部的代码
2011/12/06 Javascript
js中substring和substr的详细介绍与用法
2013/08/29 Javascript
JavaScript删除数组元素的方法
2015/03/20 Javascript
JavaScript动态修改网页元素内容的方法
2015/03/21 Javascript
javascript检查浏览器是否已经启用XX功能
2015/07/10 Javascript
js获取及修改网页背景色和字体色的方法
2015/12/29 Javascript
轻松5句话解决JavaScript的作用域
2016/07/15 Javascript
JS+CSS3模拟溢出滚动效果
2016/08/12 Javascript
jQuery zTree树插件简单使用教程
2017/01/10 Javascript
Angular之指令Directive用法详解
2017/03/01 Javascript
JS异步加载的三种实现方式
2017/03/16 Javascript
Js自定义多选框效果的实例代码
2017/07/05 Javascript
Angular4如何自定义首屏的加载动画详解
2017/07/26 Javascript
微信小程序日期时间选择器使用方法
2018/02/01 Javascript
在 webpack 中使用 ECharts的实例详解
2018/02/05 Javascript
JavaScript引用类型Date常见用法实例分析
2018/08/08 Javascript
基于Vue全局组件与局部组件的区别说明
2020/08/11 Javascript
[40:29]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第一场
2018/04/10 DOTA
python实现用于测试网站访问速率的方法
2015/05/26 Python
Python 模板引擎的注入问题分析
2017/01/01 Python
Python 通过URL打开图片实例详解
2017/06/01 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
2018/05/26 Python
在python中pandas的series合并方法
2018/11/12 Python
python执行CMD指令,并获取返回的方法
2018/12/19 Python
两种CSS3伪类选择器详细介绍
2013/12/24 HTML / CSS
客户服务经理岗位职责
2014/01/29 职场文书
店长职务说明书
2014/02/04 职场文书
让世界充满爱观后感
2015/06/10 职场文书
动视暴雪取消疫苗禁令 让所有员工返回线下工作
2022/04/03 其他游戏
如何Tomcat中使用ipv6地址
2022/05/06 Servers