python的retrying庫處理嘗試多次請求


retrying是一個python的重試包,可以用來自動重試一些可能運行失敗的程序段,retrying提供一個裝飾器函數(shù)retry,被裝飾的函數(shù)就會在運行失敗的情況下重新執(zhí)行,默認只要一直報錯就會不斷重試。

參數(shù):

  • stop_max_attempt_number:用來設(shè)定最大的嘗試次數(shù),超過該次數(shù)就會停止

  • stop_max_delay:從被裝飾的函數(shù)開始執(zhí)行的時間點開始到函數(shù)成功運行結(jié)束或失敗報錯中止的時間點。單位:毫秒

  • wait_fixed:設(shè)置在兩次retrying之間的停留時間

  • retry_on_exception:指定出現(xiàn)哪些異常的時候再去retry 例:* retry_on_exception(retry_if_io_error)

  • retry_on_result:指定要在得到哪些結(jié)果再去retry
    retrying是一個python的重試包,可以用來自動重試一些可能運行失敗的程序段,retrying提供一個裝飾器函數(shù)retry,被裝飾的函數(shù)就會在運行失敗的情況下重新執(zhí)行,默認只要一直報錯就會不斷重試。

  • stop_max_attempt_number:用來設(shè)定最大的嘗試次數(shù),超過該次數(shù)就會停止

  • stop_max_delay:從被裝飾的函數(shù)開始執(zhí)行的時間點開始到函數(shù)成功運行結(jié)束或失敗報錯中止的時間點。單位:毫秒

  • wait_fixed:設(shè)置在兩次retrying之間的停留時間

  • retry_on_exception:指定出現(xiàn)哪些異常的時候再去retry
    例:retry_on_exception(retry_if_io_error)

  • retry_on_result:指定要在得到哪些結(jié)果再去retry
    例:retry_on_result(retry_if_result_none)

功能:

  • 一般裝飾器api
  • 特定的停止條件(限制嘗試次數(shù))
  • 特定的等待條件(每次嘗試之間的指數(shù)增長的時間等待)
  • 自定義的異常進行嘗試
  • 自定義的異常進行嘗試返回結(jié)果
  • 最簡單的一個使用方法是無論有任何異常出現(xiàn),都會一直重新調(diào)用一個函數(shù)、方法,直到返回一個值
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()

運行該段代碼,你會發(fā)現(xiàn)每次隨機打印的“just have a test”這句話次數(shù)不一致
例子
正如你上邊看到的例子,默認的行為會一直重試,沒有時間等待

@retry
def never_give_up_never_surrender():
    print "Retry forever ignoring Exceptions, don't wait between retries"
  • 騷年,不要太固執(zhí),加點限制,放棄之前,嘗試幾次(代碼嘗試幾次后停止)
@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"
  • 大多數(shù)事情并不是需要盡可能快的執(zhí)行,所以加一些時間等待(每個嘗試間加固定時間等待)
@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"
  • 再一次,在重新嘗試分布式服務(wù)和其他遠程端點時,很難擊敗指數(shù)級回退(不會翻譯,大概就是每次嘗試的等待時間以指數(shù)形式增長)
@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"

我們有一些處理重新嘗試的選項,它們會引起特定的或一般的異常,就像這里的情況一樣(根據(jù)異常重試)

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 

我們也可以使用函數(shù)的結(jié)果來改變重新嘗試的行為

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/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容