python包實(shí)現(xiàn)?retrying?重復(fù)回調(diào)操作
一、安裝
- 循環(huán)、重復(fù)回調(diào)我們?cè)诤芏鄨?chǎng)景中會(huì)用到
- 不僅在支付場(chǎng)景中,我們需要通過反復(fù)的回調(diào)知道用戶的支付狀態(tài)
- 還有在請(qǐng)求中,如果請(qǐng)求失敗,我們需要再重新進(jìn)行進(jìn)行請(qǐng)求,防止請(qǐng)求異常導(dǎo)致數(shù)據(jù)缺失
pip install retrying
二、一直請(qǐng)求
- 假如我們希望在代碼碰到異常時(shí),一直回調(diào),直到成功
- 下面方法中,我們直接訪問一個(gè)未定義的變量,肯定會(huì)走下面的Exception中
- 這個(gè)時(shí)候我們可以將這一次錯(cuò)誤寫進(jìn)日志,但是讓程序繼續(xù)執(zhí)行這個(gè)方法,直到?jīng)]有異常為止
- 因?yàn)檫@里模擬的是肯定有異常,所以該程序會(huì)一直返回回調(diào),不間斷的循環(huán)往復(fù)
from retrying import retry @retry() def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
三、設(shè)置最大運(yùn)行次數(shù)
- 如果我們?cè)谡?qǐng)求中遇到異常時(shí)候
- 可以通過 stop_max_attempt_number 設(shè)置一個(gè)最大運(yùn)行次數(shù)
- 當(dāng)回調(diào)次數(shù)超過設(shè)置值,將不再執(zhí)行回調(diào)
- 這里我們?cè)O(shè)置最大運(yùn)行次數(shù)為5次
from retrying import retry @retry(stop_max_attempt_number=5) def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
四、設(shè)置重試的最大時(shí)間
- 可以通過
stop_max_delay
設(shè)置失敗重試的最大時(shí)間, 單位毫秒 - 超出時(shí)間,則停止重試
from retrying import retry @retry(stop_max_delay=1000) def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
五、設(shè)置間隔時(shí)間
- 設(shè)置失敗重試的間隔時(shí)間, 單位毫秒
- 降低回調(diào)頻率
from retrying import retry @retry(wait_fixed=1000) def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
六、設(shè)置隨機(jī)間隔時(shí)間
- 設(shè)置失敗重試隨機(jī)性間隔時(shí)間, 單位毫秒
- 可以使得訪問頻率不均勻
from retrying import retry @retry(wait_random_min=5000, wait_random_max=50000) def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
七、隨機(jī)倍數(shù)間隔時(shí)間
- 可以通過設(shè)置
wait_exponential_multiplier
間隔時(shí)間倍數(shù)增加 - 可以通過設(shè)置
wait_exponential_max
最大間隔時(shí)間
from retrying import retry @retry(wait_exponential_multiplier=1000, wait_exponential_max=10000) def say(): try: autofelix except Exception as e: # 可以將錯(cuò)誤記錄日志 print(e) raise say()
八、指定異常類型
- 可以通過
retry_on_exception
設(shè)置指定異常類型 - 指定的異常類型會(huì)重試,不指定的類型,會(huì)直接異常退出
- 如果設(shè)置
wrap_exception
參數(shù)為True
,則其他類型異常
from retrying import retry def retry_error(exception): return isinstance(exception, RetryError) # 會(huì)重復(fù)調(diào)用 @retry(etry_on_exception=retry_error) def say(): try: autofelix except RetryError as e: raise RetryError # 只調(diào)用一次 @retry(etry_on_exception=retry_error, wrap_exception=True) def say(): raise Exception('a') say()
九、過濾回調(diào)
- 可以設(shè)置
retry_on_result
指定哪些結(jié)果需要去回調(diào) - 將請(qǐng)求結(jié)果放到
retry_on_result
指定方法中進(jìn)行過濾,如果返回None,則繼續(xù)回調(diào),否則就結(jié)束
from retrying import retry def retry_filter(result): print("this is result") return result is not None @retry(retry_on_result=retry_filter) def say(): print('Retry forever ignoring Exceptions with no wait if return value is None') return None say()
十、異常執(zhí)行
- 通過設(shè)置
stop_func
每次拋出異常時(shí)都會(huì)執(zhí)行的函數(shù) - 如果和
stop_max_delay
、stop_max_attempt_number
配合使用,則后兩者會(huì)失效
from retrying import retry def stop_record(attempts, delay): print("logging %d--->%d" % (attempts,delay)) @retry(stop_max_delay=10, stop_func=stop_record) def say(): print("i am autofelix") raise Exception say()
到此這篇關(guān)于python 包之 retrying 重復(fù)回調(diào)的文章就介紹到這了,更多相關(guān)python retrying 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
正確的理解和使用Django信號(hào)(Signals)
這篇文章主要介紹了如何正確的理解和使用Django信號(hào)(Signals),幫助大家更好的理解和學(xué)習(xí)是Django,感興趣的朋友可以了解下2021-04-04numpy數(shù)組疊加的實(shí)現(xiàn)示例
本文主要介紹了numpy數(shù)組疊加的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python3實(shí)現(xiàn)Web網(wǎng)頁圖片下載
這篇文章主要介紹了Python3通過request.urlopen實(shí)現(xiàn)Web網(wǎng)頁圖片下載,感興趣的小伙伴們可以參考一下2016-01-01簡(jiǎn)單的Python人臉識(shí)別系統(tǒng)
這篇文章主要介紹了Python人臉識(shí)別系統(tǒng)的實(shí)現(xiàn),文中講解非常詳細(xì),代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07用python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu)
這篇文章主要分享的是用python實(shí)現(xiàn)各種數(shù)據(jù)結(jié)構(gòu),快速排序、選擇排序、插入排序、歸并排序、堆排序heapq模塊等相關(guān)資料,感興趣的小伙伴可以參考一下2021-12-12Python Django簡(jiǎn)單實(shí)現(xiàn)session登錄注銷過程詳解
這篇文章主要介紹了Python Django簡(jiǎn)單實(shí)現(xiàn)session登錄注銷過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08詳解Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù)及示例代碼數(shù)
本文通過示例代碼給大家詳細(xì)介紹了Python3 中hasattr()、getattr()、setattr()、delattr()函數(shù),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04