1. 程式人生 > 其它 >Python:retrying重試裝飾器的使用

Python:retrying重試裝飾器的使用

技術標籤:python

文件:https://pypi.org/project/retrying/

依賴

pip install retrying

示例

# -*- coding: utf-8 -*-

from retrying import retry

# 最多執行5次
@retry(stop_max_attempt_number=5)
def foo():
    print("foo")
    raise Exception("Exception")


if __name__ == '__main__':
    foo()

"""
輸出結果:
foo
foo
foo
foo
foo

Traceback (most recent call last):
...
Exception: Exception
"""

引數說明(待補充)

stop 
wait
stop_max_attempt_number         最大重試次數
stop_max_delay                  最大延遲時間(毫秒)
wait_fixed                      每次方法執行之間的等待時間
wait_random_min                 隨機的等待時間
wait_random_max                 隨機的等待時間
wait_incrementing_start 
wait_incrementing_increment     每呼叫一次增加固定時長
wait_exponential_multiplier 
wait_exponential_max
retry_on_exception
retry_on_result
wrap_exception
stop_func
wait_func
wait_jitter_max

參考
Python重試模組retrying