av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

關于Python錯誤重試方法總結

瀏覽:86日期:2022-06-30 15:50:27
前言

Tenacity是一個 Apache 2.0授權的通用重試庫,用 Python 編寫,用于簡化向幾乎所有內容添加重試行為的任務。它起源于一個重新嘗試的分支,可惜這個分支已經不復存在了。使用Tenacity可以用來進行測試用例的重跑,爬蟲腳本的重跑,以及搶票的失敗重搶等等。。。可以使用的場景也是比較多。

使用

首先安裝Tenacity

pip install Tenacity

無限重試

第一個重試案例,因為一直是拋出異常錯誤,所以無限進行重試執行

from tenacity import retry@retry()def test_retry():print(’失敗重試中’) raise Exception test_retry()

關于Python錯誤重試方法總結

成功則停止

我們來優化成功一次后程序則終止,否則繼續重試。

from tenacity import retryimport random@retry()def test_retry(): if random.randint(0,10) > 1: print(’失敗重試中’) raise Exception else: print(’成功’)test_retry()

關于Python錯誤重試方法總結

重試次數

畢竟一直重試需要消耗很多資源,所以我們可以設置一些重試的次數,比如在失敗多少次后停止重試,不管有沒有成功。

from tenacity import retry,stop_after_attemptimport random@retry(stop=stop_after_attempt(7))def test_retry(): # if random.randint(0,10) > 1: print(’失敗重試中’) raise Exception # else: # print(’成功’)test_retry()

關于Python錯誤重試方法總結

重試時間

也可以設置執行的時間

from tenacity import retry,stop_after_attempt,stop_after_delayimport randomfrom time import sleep@retry(stop=stop_after_delay(3))def test_retry(): # if random.randint(0,10) > 1: sleep(1) print(’失敗重試中’) raise Exception # else: # print(’成功’)test_retry()

關于Python錯誤重試方法總結

條件組合

甚至可以使用多個組合條件進行停止,哪個條件先觸發則執行哪個

from tenacity import retry,stop_after_attempt,stop_after_delayimport randomfrom time import sleep@retry(stop=stop_after_delay(3) | stop_after_attempt(2))def test_retry(): # if random.randint(0,10) > 1: sleep(1) print(’失敗重試中’) raise Exception # else: # print(’成功’)test_retry()

關于Python錯誤重試方法總結

重試間隔

重試之間的間隔時間太短,所以讓我們在重試之間等待2秒鐘

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixedimport randomimport time@retry(wait=wait_fixed(2))def test_retry(): # if random.randint(0,10) > 1: print(’失敗重試中’) print(time.ctime()) raise Exception # else: # print(’成功’)test_retry()

關于Python錯誤重試方法總結

重試隨機間隔

我們還可以設置一些等待時間范圍

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed,wait_randomimport randomimport time@retry(wait=wait_random(min=1,max=2))def test_retry(): # if random.randint(0,10) > 1: print(’失敗重試中’) print(time.ctime()) raise Exception # else: # print(’成功’)test_retry()

關于Python錯誤重試方法總結

重試前日志

在執行之前打印日志

from tenacity import retry,stop_after_attempt,before_logimport loggingimport syslogging.basicConfig(stream=sys.stderr,level=logging.DEBUG)logger = logging.getLogger(__name__)@retry(stop=stop_after_attempt(3),before=before_log(logger,logging.DEBUG))def test_retry(): print(’失敗重試中’) raise Exception(’Fail’)test_retry()

關于Python錯誤重試方法總結

重試后日志

那么相同的,可以在執行失敗后打印日志

from tenacity import retry,stop_after_attempt,after_logimport loggingimport syslogging.basicConfig(stream=sys.stderr,level=logging.DEBUG)logger = logging.getLogger(__name__)@retry(stop=stop_after_attempt(3),after=after_log(logger,logging.DEBUG))def test_retry(): print(’失敗重試中’) raise Exception(’Fail’)test_retry()

關于Python錯誤重試方法總結

基本常用的功能就這些了,如果有需要深入了解的可以訪問github地址:https://github.com/jd/tenacity

到此這篇關于關于Python錯誤重試方法總結的文章就介紹到這了,更多相關Python錯誤重試方法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲精品在线免费看 | 2018天天干天天操 | 国产精品视频播放 | 国产一级久久久久 | 在线观看av网站 | 国产在线一区二区三区 | av黄色在线观看 | 久久九| 国产精品99久久久久久久久久久久 | 久久久久亚洲精品 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 91精品国产一区二区三区 | www国产成人免费观看视频,深夜成人网 | 欧美精品国产一区二区 | 亚洲一区二区三区观看 | 91视频免费在观看 | 99国内精品 | 欧美日韩大片 | 成人av一区二区三区 | 播放一级毛片 | 国产精品久久国产精品 | 久久四虎 | 国产一区二区美女 | 一区二区三区四区免费观看 | 天天操伊人 | 精品国产精品三级精品av网址 | 亚洲精品免费在线 | 一区二区久久电影 | 男人天堂网址 | 不卡在线视频 | 一区二区三区欧美 | 国产美女特级嫩嫩嫩bbb片 | 91久久精品国产91久久 | 高清av在线 | 精品国产乱码久久久久久果冻传媒 | 久久精品国产亚洲 | 在线中文字幕第一页 | 日韩一级免费电影 | 欧美日韩成人在线 | 成人午夜激情 | 久久成人精品视频 |