使用python的retrying库处理尝试多次请求

功能

  1. 一般装饰器api
  2. 特定的停止条件(限制尝试次数)
  3. 特定的等待条件(每次尝试之间的指数增长的时间等待)
  4. 自定义的异常进行尝试
  5. 自定义的异常进行尝试返回结果
    最简单的一个使用方法是无论有任何异常出现,都会一直重新调用一个函数、方法,直到返回一个值
import random
from retrying import retry

@retry
def do_something_unreliable():
    if random.randint(0, 10) > 1:
        print "just have a test"
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print do_something_unreliable()

运行该段代码,你会发现每次随机打印的“just have a test”这句话次数不一致
安装

简单的安装retrying:

pip install retrying

或者你也可以这样,但是你可能会后悔(没明白为什么。。。):

easy_install retrying

例子
正如你上边看到的例子,默认的行为会一直重试,没有时间等待

@retry
def never_give_up_never_surrender():
    print "Retry forever ignoring Exceptions, don't wait between retries"

骚年,不要太固执,加点限制,放弃之前,尝试几次(代码尝试几次后停止)

@retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
    print "Stopping after 7 attempts"

我们没有太多的时间,所以在每个尝试需要加个时间限制(多少s后停止尝试)

@retry(stop_max_delay=10000)
def stop_after_10_s():
    print "Stopping after 10 seconds"

大多数事情并不是需要尽可能快的执行,所以加一些时间等待(每个尝试间加固定时间等待)

@retry(wait_fixed=2000)
def wait_2_s():
    print "Wait 2 second between retries"

一些最好是随机的时间等待(每个尝试随机时间等待)

@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print "Randomly wait 1 to 2 seconds between retries"

再一次,在重新尝试分布式服务和其他远程端点时,很难击败指数级回退(不会翻译,大概就是每次尝试的等待时间以指数形式增长)

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
    print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"

我们有一些处理重新尝试的选项,它们会引起特定的或一般的异常,就像这里的情况一样(根据异常重试)

def retry_if_io_error(exception):
    """Return True if we should retry (in this case when it's an IOError), False otherwise"""
    return isinstance(exception, IOError)

@retry(retry_on_exception=retry_if_io_error)
def might_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors"

@retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors wrapped in RetryError"

我们也可以使用函数的结果来改变重新尝试的行为

def retry_if_result_none(result):
    """Return True if we should retry (in this case when result is None), False otherwise"""
    return result is None

@retry(retry_on_result=retry_if_result_none)
def might_return_none():
    print "Retry forever ignoring Exceptions with no wait if return value is None"

任何停止、等待等的组合也会被支持,使你可以自由地混合和匹配。骚男,尝试起来吧!
原文:https://pypi.org/project/retrying/

猜你喜欢

转载自blog.csdn.net/lb245557472/article/details/80782491