Python 官方文档:入门教程 => 点击学习
目录一、安装二、一直请求三、设置最大运行次数四、设置重试的最大时间五、设置间隔时间六、设置随机间隔时间七、随机倍数间隔时间八、指定异常类型九、过滤回调十、异常执行一、安装 循环、重复
pip install retrying
from retrying import retry
@retry()
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
from retrying import retry
@retry(stop_max_attempt_number=5)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
stop_max_delay
设置失败重试的最大时间, 单位毫秒from retrying import retry
@retry(stop_max_delay=1000)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
from retrying import retry
@retry(wait_fixed=1000)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
from retrying import retry
@retry(wait_random_min=5000, wait_random_max=50000)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
wait_exponential_multiplier
间隔时间倍数增加wait_exponential_max
最大间隔时间from retrying import retry
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
retry_on_exception
设置指定异常类型wrap_exception
参数为True
,则其他类型异常from retrying import retry
def retry_error(exception):
return isinstance(exception, RetryError)
# 会重复调用
@retry(etry_on_exception=retry_error)
def say():
try:
autofelix
except RetryError as e:
raise RetryError
# 只调用一次
@retry(etry_on_exception=retry_error, wrap_exception=True)
def say():
raise Exception('a')
say()
retry_on_result
指定哪些结果需要去回调retry_on_result
指定方法中进行过滤,如果返回None,则继续回调,否则就结束from retrying import retry
def retry_filter(result):
print("this is result")
return result is not None
@retry(retry_on_result=retry_filter)
def say():
print('Retry forever ignoring Exceptions with no wait if return value is None')
return None
say()
stop_func
每次抛出异常时都会执行的函数stop_max_delay
、stop_max_attempt_number
配合使用,则后两者会失效from retrying import retry
def stop_record(attempts, delay):
print("logging %d--->%d" % (attempts,delay))
@retry(stop_max_delay=10, stop_func=stop_record)
def say():
print("i am autofelix")
raise Exception
say()
到此这篇关于python 包之 retrying 重复回调的文章就介绍到这了,更多相关Python retrying 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python包实现 retrying 重复回调操作
本文链接: https://www.lsjlt.com/news/117351.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0