python retry feature

简单的retry 功能有两种实现方法
  • retry module - 可以实现简单的retry, 指定retry 次数。
  • backoff module - 相对于retry 模块, 功能更丰富一点。 
retry example
1 @retry(InvalidLogin, tries=2, delay=0)
2 @retry(httpx.ConnectError, tries=5, delay=2) # decorator 可以嵌套使用
3 def get(cls, endpoint: str):
4     。。。。
View Code

backoff example

1 @backoff.on_exception(
2     backoff.constant,
3     Exception,
4     max_tries=3,
5     giveup=fatal_code,
6     on_backoff=backoff_fun,
7 )
8 def test2():
9    ...
View Code

通过查看backoff onexception 的signature, 学习下用法啊

"""Returns decorator for backoff and retry triggered by exception.


Args:
    wait_gen: A generator yielding successive wait times in
        seconds. ## 一个generator, 可以before retry需要等待的时间。
    exception: An exception type (or tuple of types) which triggers
        backoff. # 显而易见, exception or tuple of exception
    max_tries: The maximum number of attempts to make before giving
        up. Once exhausted, the exception will be allowed to escape.
        The default value of None means there is no limit to the
        number of tries. If a callable is passed, it will be
        evaluated at runtime and its return value used.
    max_time: The maximum total amount of time to try for before
        giving up. Once expired, the exception will be allowed to
        escape. If a callable is passed, it will be
        evaluated at runtime and its return value used.
    jitter: A function of the value yielded by wait_gen returning
        the actual time to wait. This distributes wait times
        stochastically in order to avoid timing collisions across
        concurrent clients. Wait times are jittered by default
        using the full_jitter function. Jittering may be disabled
        altogether by passing jitter=None.
    giveup: Function accepting an exception instance and
        returning whether or not to give up. Optional. The default
        is to always continue.  # giveup=fatal_code
```
def fatal_code(e): # 入参是exception instance
    # return type(e) == 'TypeError'
    e_type = type(e)
    logger.info(f"Give up code. error is - {e_type} - {str(e)} ..")
    return 0 # 返回0 or 1, 0: retry 继续, 不giveup
```
    on_success: Callable (or iterable of callables) with a unary
        signature to be called in the event of success. The
        parameter is a dict containing details about the invocation.
    on_backoff: Callable (or iterable of callables) with a unary
        signature to be called in the event of a backoff. The
        parameter is a dict containing details about the invocation.
```
这三个on function, 都是类似的用法, backoff的时候做什么, 用法on_backoff=backoff_fun
def backoff_fun(detail): # detail is a dict, 包括你需要的所有的东西
    logger.info(f"backoff function {detail}")
    return 1
```
    on_giveup: Callable (or iterable of callables) with a unary
        signature to be called in the event that max_tries
        is exceeded.  The parameter is a dict containing details
        about the invocation.
    raise_on_giveup: Boolean indicating whether the registered exceptions
        should be raised on giveup. Defaults to `True`
    logger: Name or Logger object to log to. Defaults to 'backoff'.
    backoff_log_level: log level for the backoff event. Defaults to "INFO"
    giveup_log_level: log level for the give up event. Defaults to "ERROR"
    **wait_gen_kwargs: Any additional keyword args specified will be
        passed to wait_gen when it is initialized.  Any callable
        args will first be evaluated and their return values passed.
        This is useful for runtime configuration.

 

posted on 2023-02-13 15:09  MissLi12138  阅读(32)  评论(0编辑  收藏  举报

导航