轻量级的pythonic重试助手
tryagain的Python项目详细描述
尝试
一个轻量级的python重试助手。
tryagain旨在简化不稳定函数的工作。是否 您的网络代码有时会引发超时异常,或者 控制设备似乎只在第二次尝试时监听- tryagain使重复调用更容易。
tryagain在尝试失败后提供要清理的钩子,或者 准备下一个电话。您可以设置重试之间的等待时间,或者 指定自己的等待时间函数以实现指数等待时间等。
tryagain是轻量级的,经过全面测试的,麻省理工学院授权的,并且作为一个 没有依赖项的python文件。它支持Python2.6+和3.2+。
要安装,请运行pip install tryagain。
基本语法
使用tryagain函数call:
importtryagaindefunstable_function():# Attention: This function sometimes fails!...result=tryagain.call(unstable_function,max_attempts=None,exceptions=Exception,wait=0.0,cleanup_hook=None,pre_retry_hook=None)
使用tryagain装饰符retries:
fromtryagainimportretries@retries(max_attempts=3)defunstable_funcation(arg1,arg2):# Attention: This function sometimes fails!...result=unstable_function('foo',arg2='bar')
参数
- func:要调用的不稳定函数
- ^ TT9}$:任意整数以限制最大数目 尝试。设置为无限制重试。(默认值=无)
- exceptions:应该导致 重试。(默认值=Exception)
- wait:可以是整数或浮点数(以秒为单位指定等待时间)或自定义函数(请参阅等待时间文档)(默认值=0.0)
- cleanup_hook:可以设置为可调用,并在之后调用 调用func时引发异常。(默认值=无)
- pre_retry_hook:可以设置为将被调用的任何可调用的 在调用func之前。(默认值=无)
结果
tryagain.call将返回不稳定函数将返回的任何内容 返回。tryagain.call(和装饰者tryagain.retries)重新访问 任何例外情况是:
- 不在给定的exceptions
- 在pre_retry_hook或cleanup_hook 中引发
- 在上次尝试调用不稳定函数时引发。
快速启动
重试调用不稳定的函数
importtryagaindefunstable():...# retry calling 'unstable' until it returns without raising an exceptionresult=tryagain.call(unstable)# limit to maximum 5 attemptsresult=tryagain.call(unstable,max_attempts=5)# only retry after specific exceptionsresult=tryagain.call(unstable,exceptions=(ValueError,TypeError))
等待时间
tryagain库允许固定的等待值和自定义的等待时间 功能。
# wait one second before trying againtryagain.call(unstable,wait=1.0)# waittime rises linearly (n is the number of attempts)# (will wait 1s, 2s, 3s, ...)tryagain.call(unstable,wait=lambdan:n)# waittime rises exponentially with each attempt# (will wait 2s, 4s, 8s, ...)tryagain.call(unstable,wait=lambdan:2**n)# exponentially rising waittime with maximum# (will wait 2s, 4s, 5s, 5s, ..., 5s)tryagain.call(unstable,wait=lambdan:min(n**2,5))# no waiting time before second attempt, 1.0s afterwardsdefno_first_wait(attempt):ifattempt==2:return0else:return1.0tryagain.call(unstable,wait=no_first_wait)
使用参数重试调用函数
tryagain.call-函数只支持函数引用作为 func参数。要将参数传递给必须使用的不稳定函数 下列成语之一:
# using a lambdatryagain.call(lambda:unstable('message',some_arg=True),wait=1.0)# using a partialfromfunctoolsimportpartialtryagain.call(partial(unstable,'message',some_arg=True),wait=1.0)# using a separate functiondefcall_unstable_function():msg='message'returnunstable(msg,some_arg=True)tryagain.call(call_unstable_function,wait=1.0)
但是将不稳定的函数包装在^{tt25}中会更好。$ 装饰工。 这样,您可以使用参数轻松调用不稳定函数:
函数装饰器
不使用tryagain.call函数,您可以使用retries 装饰工。
fromtryagainimportretries@retries(max_attempts=3,exceptions=(TypeError,ValueError))defunstable(arg1,arg2):# your unstable function hereresult=unstable('foo',arg2='bar')
decorator采用与call-函数相同的参数 除了func参数。
挂钩
Tryagain库有两个可以使用的钩子, cleanup_hook和pre_retry_hook。
defunstable():print('Calling unstable function')print('Exception!')raiseExceptiontryagain.call(unstable,max_attempts=2,wait=lambdan:print('waiting'),cleanup_hook=lambda:print('cleaning up'),pre_retry_hook=lambda:print('do preparations'))'Calling unstable function''Exception!''cleaning up''waiting''do preparations''Calling unstable function''Exception!''cleaning up'Error:Exceptionraised...