Board logo

标题: retry装饰器 [打印本页]

作者: admin    时间: 2019-9-23 20:58     标题: retry装饰器

Windows安装第三方库:(retry) C:\Users\jacky\Desktop> pip3 install retry -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com Windows卸载第三方库:(retry) C:\Users\jacky>pip3 uninstall retry -y Uninstalling retry-0.9.2: Successfully uninstalled retry-0.9.2 ### 例子一:(当retry装饰器无参数时,遇到异常会一直重试,直到成功为止) #coding=utf-8 from retry import retry index_1 = 0 @retry() #如果遇到异常,会一直重试,直到成功为止 def func1(): global index_1 index_1 = index_1 + 1 if index_1 >= 5: print(f'执行到第{index_1}次时,没有异常了') else: print('异常') raise #引发一次异常 if __name__ == '__main__': func1() print("-" * 10) print(index_1) 脚本运行的结果: C:\Users\jacky\Desktop>python xx.py 异常 异常 异常 异常 执行到第5次时,没有异常了 ---------- 5 C:\Users\jacky\Desktop> ### 例子二: #coding=utf-8 from retry import retry import random index_1 = 0 @retry() def func1(): global index_1 Key_1 = random.choice(['zhuohua','Python',123,168]) #随机显示列表里的某个元素 index_1 = index_1 + 1 if Key_1 == 'zhuohua': print(f"第{index_1}次时找到元素'{Key_1}'") else: print('异常') raise #引发一次异常 if __name__ == '__main__': func1() 脚本运行的结果:(由于是随机显示列表里的某个元素,所以每次的结果都可能不一样) C:\Users\jacky\Desktop>python xx.py 异常 异常 异常 异常 异常 异常 第7次时找到元素'zhuohua' C:\Users\jacky\Desktop> ### 例子三: #coding=utf-8 from retry import retry import datetime @retry() #当D:\1.txt不存在时,会一直重试,直到成功为止 def func1(): #检测文件D:\1.txt是否存在 Path_1 = "D:/1.txt" f_name = open(Path_1) print("文件D:\\1.txt打开成功") f_name.close() def func2(): #把成功的输出结果写入(覆盖)到文件D:\2.txt dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d_%H%M%S') Result = f"检测时间:{tt} 文件D:\\1.txt已经存在了" Path_1 = r'D:/2.txt' #文件D:\2.txt不存在时,会自动创建 f_name = open(Path_1,'w') print(Result,file=f_name) if __name__ == '__main__': func1() #假如func1()没有执行成功,就不会执行func2() func2() 脚本运行的效果:(D:\1.txt不存在时;会一直重试,直到成功为止;组合键Ctrl+c可结束) 图片1.png 备注:文件D:\1.txt不存在时,不会把记录数据写到文件D:\2.txt 脚本运行的效果:(D:\1.txt存在时;由于是覆盖,所以就算多次运行脚本,文件D:\2.txt也只会有一条记录数据) 图片2.png 文件D:\2.txt的记录数据: 检测时间:20190208_143415 文件D:\1.txt已经存在了 ### 例子四:(当retry装饰器有参数时) #coding=utf-8 from retry import retry import datetime index_1 = 0 @retry(tries=3,delay=2) #出现错误时,就重试3次,每次尝试之间间隔2秒;重试到第3次时,依旧出现错误就引发异常。 def func1(): #假如文件D:\1.txt不存在,就会引发异常 global index_1 index_1 = index_1 + 1 print(f"第{index_1}次执行") Path_1 = "D:/1.txt" f_name = open(Path_1) print("文件D:\\1.txt打开成功") f_name.close() def func2(): #把成功的输出结果写入(追加)到文件D:\2.txt dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d_%H%M%S') Result = f"检测时间:{tt} 文件D:\\1.txt已经存在" Path_1 = r'D:/2.txt' #文件D:\2.txt不存在时,会自动创建 f_name = open(Path_1,'a') print(Result,file=f_name) if __name__ == '__main__': func1() func2() 脚本运行的效果:(D:\1.txt不存在时) C:\Users\jacky\Desktop>python xx.py 第1次执行 第2次执行 第3次执行 Traceback (most recent call last): File "xx.py", line 30, in func1() File "C:\Users\jacky\AppData\Local\Programs\Python\Python36-32\lib\site-packages\decorator.py", line 232, in fun return caller(func, *(extras + args), **kw) File "C:\Users\jacky\AppData\Local\Programs\Python\Python36-32\lib\site-packages\retry\api.py", line 74, in retry_decorator logger) File "C:\Users\jacky\AppData\Local\Programs\Python\Python36-32\lib\site-packages\retry\api.py", line 33, in __retry_internal return f() File "xx.py", line 16, in func1 f_name = open(Path_1) FileNotFoundError: [Errno 2] No such file or directory: 'D:/1.txt' C:\Users\jacky\Desktop> 备注:文件D:\1.txt不存在时,不会把记录数据写到文件D:\2.txt 多次运行脚本的效果:(D:\1.txt存在时;由于是追加,所以多次运行脚本,文件D:\2.txt就会有多条记录数据) 图片3.png 文件D:\2.txt的记录数据: 检测时间:20190208_152202 文件D:\1.txt已经存在 检测时间:20190208_152311 文件D:\1.txt已经存在 检测时间:20190208_152405 文件D:\1.txt已经存在 相关文章: try语句 for循环语句 Python3使用Virtual Environment CentOS8使用Socket(检测TCP端口) 文件操作(创建、读取、写入、追加)

图片附件: 图片1.png (2022-2-8 16:59, 3.93 KB) / 下载次数 106
http://blog.zhuohua.store/attachment.php?aid=19094&k=7db5a617884a9388ddbec75bcc043af9&t=1714924021&sid=6l88zE



图片附件: 图片2.png (2022-2-8 16:59, 4.88 KB) / 下载次数 111
http://blog.zhuohua.store/attachment.php?aid=19095&k=591a473ba017815b9c68ed021be5591d&t=1714924021&sid=6l88zE



图片附件: 图片3.png (2022-2-8 17:00, 8.65 KB) / 下载次数 114
http://blog.zhuohua.store/attachment.php?aid=19096&k=eb6f88a1e3e1f7315af1d0a597f73e66&t=1714924021&sid=6l88zE






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2