国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

關(guān)于Python錯(cuò)誤重試方法總結(jié)

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

Tenacity是一個(gè) Apache 2.0授權(quán)的通用重試庫(kù),用 Python 編寫,用于簡(jiǎn)化向幾乎所有內(nèi)容添加重試行為的任務(wù)。它起源于一個(gè)重新嘗試的分支,可惜這個(gè)分支已經(jīng)不復(fù)存在了。使用Tenacity可以用來進(jìn)行測(cè)試用例的重跑,爬蟲腳本的重跑,以及搶票的失敗重?fù)尩鹊取!!?梢允褂玫膱?chǎng)景也是比較多。

使用

首先安裝Tenacity

pip install Tenacity

無限重試

第一個(gè)重試案例,因?yàn)橐恢笔菕伋霎惓ee(cuò)誤,所以無限進(jìn)行重試執(zhí)行

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

關(guān)于Python錯(cuò)誤重試方法總結(jié)

成功則停止

我們來優(yōu)化成功一次后程序則終止,否則繼續(xù)重試。

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

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試次數(shù)

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

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試時(shí)間

也可以設(shè)置執(zhí)行的時(shí)間

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

條件組合

甚至可以使用多個(gè)組合條件進(jìn)行停止,哪個(gè)條件先觸發(fā)則執(zhí)行哪個(gè)

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試間隔

重試之間的間隔時(shí)間太短,所以讓我們?cè)谥卦囍g等待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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試隨機(jī)間隔

我們還可以設(shè)置一些等待時(shí)間范圍

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試前日志

在執(zhí)行之前打印日志

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

重試后日志

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

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()

關(guān)于Python錯(cuò)誤重試方法總結(jié)

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

到此這篇關(guān)于關(guān)于Python錯(cuò)誤重試方法總結(jié)的文章就介紹到這了,更多相關(guān)Python錯(cuò)誤重試方法內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 德庆县| 紫云| 汉寿县| 登封市| 德阳市| 如皋市| 金塔县| 鹰潭市| 株洲县| 五常市| 夏河县| 武义县| 桓仁| 金寨县| 增城市| 东海县| 南召县| 广南县| 凤翔县| 大港区| 嵊州市| 宜兴市| 微山县| 溆浦县| 兴宁市| 霍州市| 道真| 临沂市| 托里县| 镇平县| 蒙自县| 缙云县| 噶尔县| 来安县| 博爱县| 黄平县| 错那县| 两当县| 托克逊县| 綦江县| 元阳县|