重试操作的最简单方法

rtr的Python项目详细描述


rtry

LicenseDroneCodecovPyPIPython Version

安装

pip3 install rtry

文档


超时

作为上下文管理器
fromrtryimporttimeout,CancelledErrortry:withtimeout(3.0):resp=requests.get("https://httpbin.org/status/200")exceptCancelledError:raiseelse:print(resp)
作为上下文管理器(静默)
fromrtryimporttimeout,CancelledErrorresp=Nonewithtimeout(3.0,exception=None):resp=requests.get("https://httpbin.org/status/200")
作为装饰工
fromrtryimporttimeout,CancelledError@timeout(3.0)deffn():resp=requests.get("https://httpbin.org/status/200")returnresptry:resp=fn()exceptCancelledError:raiseelse:print(resp)
作为参数
fromrtryimportretry,CancelledError@retry(until=lambdar:r.status_code!=200,timeout=3.0)deffn():resp=requests.get("https://httpbin.org/status/200")returnresptry:resp=fn()exceptCancelledError:raiseelse:print(resp)

重试

尝试

@retry(attempts=2)deffn():resp=requests.get("https://httpbin.org/status/500")print(resp)assertresp.status_code==200returnrespresp=fn()# <Response [500]># <Response [500]># Traceback:#   AssertionError

直到

@retry(until=lambdar:r.status_code!=200,attempts=2)deffn():resp=requests.get("https://httpbin.org/status/500")print(resp)returnrespresp=fn()# <Response [500]># <Response [500]>

记录器

简单的记录器
@retry(until=lambdar:r.status_code!=200,attempts=2,logger=print)deffn():resp=requests.get("https://httpbin.org/status/500")returnrespresp=fn()# 1 <Response [500]> <function fn at 0x103dcd268># 2 <Response [500]> <function fn at 0x103dcd268>
自定义记录器
deflogger(attempt,result_or_exception,decorated):logging.info("Attempt: %d, Result: %s",attempt,result_or_exception)@retry(until=lambdar:r.status_code!=200,attempts=2,logger=logger)deffn():resp=requests.get("https://httpbin.org/status/500")returnrespresp=fn()# INFO:root:Attempt: 1, Result: <Response [500]># INFO:root:Attempt: 2, Result: <Response [500]>

延迟

常量延迟
@retry(until=lambdar:r.status_code!=200,attempts=2,delay=0.1)deffn():resp=requests.get("https://httpbin.org/status/500")returnrespstarted_at=time()resp=fn()ended_at=time()print('Elapsed {:.2f}'.format(ended_at-started_at))# Elapsed 2.11
自定义延迟
frommathimportexp@retry(until=lambdar:r.status_code!=200,attempts=2,delay=exp)deffn():resp=requests.get("https://httpbin.org/status/500")returnrespstarted_at=time()resp=fn()ended_at=time()print('Elapsed {:.2f}'.format(ended_at-started_at))# Elapsed 11.79

吞咽

第一次异常时失败
@retry(attempts=2,swallow=None,logger=print)deffn():resp=requests.get("http://127.0.0.1/status/500")returnresptry:resp=fn()exceptExceptionase:print(e)# HTTPConnectionPool(host='127.0.0.1', port=80): Max retries exceeded with url: /status/500
仅吞咽连接错误
fromrequests.exceptionsimportConnectionError@retry(attempts=2,swallow=ConnectionError,logger=print)deffn():resp=requests.get("http://127.0.0.1/status/500")returnresptry:resp=fn()exceptExceptionase:print(e)# 1 HTTPConnectionPool(host='127.0.0.1', port=80): Max retries exceeded with url: /status/500# 2 HTTPConnectionPool(host='127.0.0.1', port=80): Max retries exceeded with url: /status/500# HTTPConnectionPool(host='127.0.0.1', port=80): Max retries exceeded with url: /status/500

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
google chrome我在哪里可以下载Mac OSX Lion的Java 6?   java管道化hadoop映射减少作业   java避免在使用jsoup解析html时删除空格和换行符   java将arrayList中的元素相互比较   如何创建一个计算一组数字之和的程序(Java)?   java JavaFX 8 JVM在退出后保留   升华文本3升华文本3 Java交互控制台?   JAVAutil。扫描仪和维基百科   java Android在RecyclerView中更新TextClock的时间格式(12/24小时格式)   java集合,如果达到限制,则可以在添加新项之前删除旧项   hex Java将十六进制转换为带符号的8位代码   java如何使用SQL中其他列的数据?   java如何确保返回指定类型的列表?   列出如何在java中声明匿名arraylist?   在JavaSpringBoot中使用RESTXML文件   使用Java在ColdFusion中计算HMACSHA256摘要的加密