python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python 中的 retrying

python包实现 retrying 重复回调操作

作者:autofelix 

这篇文章主要介绍了python包实现 retrying 重复回调操作,文章python的相关资料展开对retrying 重复回调的详细介绍,需要的小伙伴可以参考一下,希望对你的学习有所帮助

一、安装

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

四、设置重试的最大时间

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

七、随机倍数间隔时间

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

八、指定异常类型

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

九、过滤回调

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

十、异常执行

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 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文