zoukankan      html  css  js  c++  java
  • retrying模块

    API 介绍

     1   def __init__(self,
     2                  stop=None, wait=None,
     3                  stop_max_attempt_number=None,
     4                  stop_max_delay=None,
     5                  wait_fixed=None,
     6                  wait_random_min=None, wait_random_max=None,
     7                  wait_incrementing_start=None, wait_incrementing_increment=None,
     8                  wait_exponential_multiplier=None, wait_exponential_max=None,
     9                  retry_on_exception=None,
    10                  retry_on_result=None,
    11                  wrap_exception=False,
    12                  stop_func=None,
    13                  wait_func=None,
    14                  wait_jitter_max=None)
    • stop_max_attempt_number:用来设定最大的尝试次数,超过该次数就停止重试
    • stop_max_delay:比如设置成10000,那么从被装饰的函数开始执行的时间点开始,到函数成功运行结束或者失败报错中止的时间点,只要这段时间超过10秒,函数就不会再执行了
    • wait_fixed:设置在两次retrying之间的停留时间
    • wait_random_min和wait_random_max:用随机的方式产生两次retrying之间的停留时间
    • wait_exponential_multiplier和wait_exponential_max:以指数的形式产生两次retrying之间的停留时间,产生的值为2^previous_attempt_number * wait_exponential_multiplier,previous_attempt_number是前面已经retry的次数,如果产生的这个值超过了wait_exponential_max的大小,那么之后两个retrying之间的停留值都为wait_exponential_max
    • 我们可以指定要在出现哪些异常的时候再去retry,这个要用retry_on_exception传入一个函数对象

    例子:

    • @retry装饰器,如出现异常会一直重试
    1 @retry
    2 def never_give_up_never_surrender():
    3     print "Retry forever ignoring Exceptions, don't wait between retries"
    • stop_max_attempt_number 设置最大重试次数
    1 @retry(stop_max_attempt_number=7)
    2 def stop_after_7_attempts():
    3     print "Stopping after 7 attempts"
    4     raise
    • stop_max_delay 设置失败重试的最大时间, 单位毫秒,超出时间,则停止重试
    1 @retry(stop_max_delay=10000)
    2 def stop_after_10_s():
    3     print "Stopping after 10 seconds"
    4     raise
    • wait_fixed 设置失败重试的间隔时间
    @retry(wait_fixed=2000, stop_max_delay=10000)
    def wait_2_s():
        print "Wait 2 second between retries"
        raise
    • wait_random_min, wait_random_max 设置失败重试随机性间隔时间
    1 @retry(wait_random_min=1000, wait_random_max=5000, stop_max_delay=10000)
    2 def wait_random_1_to_5_s():
    3     print "Randomly wait 1 to 5 seconds between retries"
    4     raise
    • wait_exponential_multiplier-间隔时间倍数增加,wait_exponential_max-最大间隔时间
    import time
    
    @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"
        print int(time.time())
        raise


    输出:
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110314
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110316
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110320
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110328
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110338
    Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards
    1504110348
    • retry_on_exception指定异常类型,指定的异常类型会重试,不指定的类型,会直接异常退出,wrap_exception参数设置为True,则其他类型异常,或包裹在RetryError中,会看到RetryError和程序抛的Exception error
    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"
        raise Exception('a')
    
    
    @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"
        raise Exception('a')
    • retry_on_result, 指定要在得到哪些结果的时候去retry,retry_on_result传入一个函数对象,在执行get_result成功后,会将函数的返回值通过形参result的形式传入retry_if_result_none函数中,如果返回值是None那么就进行retry,否则就结束并返回函数值
    1 def retry_if_result_none(result):
    2     return result is None
    3 
    4 @retry(retry_on_result=retry_if_result_none)
    5 def get_result():
    6     print 'Retry forever ignoring Exceptions with no wait if return value is None'
    7     return None

    转载自:https://www.cnblogs.com/wangwei916797941/articles/10370062.html

    
    
    
    
    
    
  • 相关阅读:
    关于Action中ValidateXXX方法校验一次失败后\导致以后一直返回input视图的情况
    but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)
    个人学习笔记MyBatis的搭建及第一个程序
    Hibernate学习笔记环境搭建及运行
    个人学习笔记MyBatis官方推荐DAO开发方案
    个人笔记struts2对Action的权限拦截
    hession
    正向代理,反向代理
    path,classpath
    session
  • 原文地址:https://www.cnblogs.com/GouQ/p/13543266.html
Copyright © 2011-2022 走看看