python重试实用程序
retryp的Python项目详细描述
一种自动重试函数的简单方法 提出错误。您可以直接调用retry或装饰 函数使其重试。智能匹配逻辑允许您 在引发其他异常时重试某个异常。
直接调用函数:
from retrypy import retry, check def dummy_func(): print "dummy_func called..." raise Exception("House") retry.call( dummy_func, times=2 ) dummy_func called... dummy_func called... Exception: House # usign a check method retry.call( dummy_func, check = check.message_equals("foobar") ) dummy_func called... Exception: House
修饰函数:
# Only retry IOErrors @retry.decorate(IOError, times=2) def dummy_func(): print "dummy_func called..." raise IOError("House") dummy_func() dummy_func called... dummy_func called... IOError: House # Retry any Exception, use a custom wait function @retry.decorate(times=2, wait=lambda n: 2*n) def dummy_func(): print "dummy_func called..." raise Exception("House") dummy_func() dummy_func called... dummy_func called... Exception: House
包装一个函数并返回一个新的可调用函数:
def dummy_func(): print "dummy_func called..." raise Exception("House") func = retry.wrap( dummy_func, times=2 ) func() dummy_func called... dummy_func called... Exception: House
延迟助手:
实现一些常见退避策略的函数:随机、指数和增量。
随机:
import time from retrypy import retry, delay def dummy_func(): print time.time() raise Exception("House") retry.call(dummy_func, wait=delay.random( min_seconds=1, max_seconds=5, ))
输出(等待时间:.66s、.84s、3.42s、.33s):
1423297137.49 1423297138.15 1423297138.99 1423297142.41 1423297142.74 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "retrypy/retry.py", line 63, in call wait, File "retrypy/retry.py", line 29, in _retry raise previous_exception Exception: House
指数型:
import time from retrypy import retry, delay def dummy_func(): print time.time() raise Exception("House") retry.call(dummy_func, wait=delay.exponential( start_at=1, ))
输出(等待时间:1s、2s、4s、8s):
1423297238.49 1423297239.49 1423297241.49 1423297245.5 1423297253.5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "retrypy/retry.py", line 63, in call wait, File "retrypy/retry.py", line 29, in _retry raise previous_exception Exception: House
增量:
import time from retrypy import retry, delay def dummy_func(): print time.time() raise Exception("House") retry.call(dummy_func, wait=delay.incremental( start_at=1, step=1, ))
输出(等待时间:1s、2s、3s、4s):
1423297301.64 1423297302.64 1423297304.64 1423297307.65 1423297311.65 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "retrypy/retry.py", line 63, in call wait, File "retrypy/retry.py", line 29, in _retry raise previous_exception Exception: House
自定义延迟功能:
您可以编写自己的延迟函数,它们的唯一要求是它们需要Integer并返回Number秒的等待时间。
def custom_delay(call_count): if call_count == 1: # don't wait at all the first time return 0 # wait 4, 8, 16, 32 return 2 ** call_count
内置异常检查程序:
异常检查程序可用于检查是否要重试特定异常。如果check函数返回true,那么异常是可重试的,否则我们将不会捕获异常并重试。可用的checker是:message_equals、message_contains和message_matches。
message_等于,将任何Exception与提供的字符串相同的消息匹配。
message\u contains,将任何Exception与包含所提供字符串的消息匹配。
message\u matches,将任何Exception与提供的regex匹配的消息匹配。regex可以作为字符串或编译的regex模式传递。
自定义异常检查程序:
您可以编写自己的异常检查程序,它们的唯一要求是:接受一个Exception和一个Integer作为参数。如果异常可重试,则它们应返回true,否则返回false。
def custom_matcher(e, call_count): # never fail their first time no matter what if call_count == 1: return True # only retry errors with Bob Barker in the message. return "Bob Barker" in str(e):
安装:
>> pip install retrypy
开发:
>> git clone https://github.com/toddsifleet/retrypy >> cd retrypy >> make bootstrap >> make
许可证:
请参见许可证