最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年01月03日 09:22:30   作者:Huny  
在本篇文章里小編給網(wǎng)友們分享一篇關(guān)于關(guān)于Python錯誤重試方法總結(jié)內(nèi)容,有需要的朋友們跟著學習參考下。

前言

Tenacity是一個 Apache 2.0授權(quán)的通用重試庫,用 Python 編寫,用于簡化向幾乎所有內(nèi)容添加重試行為的任務。它起源于一個重新嘗試的分支,可惜這個分支已經(jīng)不復存在了。
使用Tenacity可以用來進行測試用例的重跑,爬蟲腳本的重跑,以及搶票的失敗重搶等等。。??梢允褂玫膱鼍耙彩潜容^多。

使用

首先安裝Tenacity

pip install Tenacity

無限重試

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

from tenacity import retry

@retry()
def test_retry():
	print('失敗重試中')
 raise Exception
 
test_retry()

成功則停止

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

from tenacity import retry
import random

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

test_retry()

重試次數(shù)

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

from tenacity import retry,stop_after_attempt
import random

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

test_retry()

重試時間

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

from tenacity import retry,stop_after_attempt,stop_after_delay
import random
from 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()

條件組合

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

from tenacity import retry,stop_after_attempt,stop_after_delay
import random
from 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()

重試間隔

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

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed
import random
import 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()

重試隨機間隔

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

from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed,wait_random
import random
import 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()

重試前日志

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

from tenacity import retry,stop_after_attempt,before_log
import logging
import sys

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

重試后日志

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

from tenacity import retry,stop_after_attempt,after_log
import logging
import sys

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

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

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

相關(guān)文章

最新評論

福贡县| 平阳县| 当阳市| 祁东县| 伊金霍洛旗| 讷河市| 深水埗区| 娄烦县| 柳江县| 潼关县| 卓资县| 武清区| 洞头县| 汤阴县| 章丘市| 荔浦县| 临海市| 北票市| 青田县| 隆子县| 基隆市| 钟山县| 和顺县| 桐城市| 调兵山市| 富蕴县| 金阳县| 隆昌县| 平乐县| 文安县| 昌吉市| 屏山县| 汝城县| 响水县| 迁安市| 奉新县| 通道| 三穗县| 高雄市| 漾濞| 清水河县|