Python爬虫还在写重试代码?快快学习下优雅的tenacity库!
小詹学Python
共 3827字,需浏览 8分钟
·
2021-01-18 12:55
前言
pip install tenacity
无条件重试,重试之间无间隔
import tenacity
import requests
# 直接加上retry装饰器,代码抛出异常会一直重试,直到代码运行成功
@tenacity.retry()
def baidu():
response = requests.get(url = 'http://www.baidu.com')
if response.status_code == 200:
return response.text
raise Exception
res = baidu()
print(res)
from tenacity import retry , stop_after_attempt , stop_after_delay , wait_fixed
import requests
# 在程序重试前固定等待时间
@retry(wait = wait_fixed(3))
def baidu():
web = requests.get(url = 'http://www.baidu.com')
if web.status_code == 200:
return web.text
raise Exception
res = baidu()
print(res)
设置停止的基本条件
只重试3次
from tenacity import retry,stop_after_attempt
import requests
# 加上终止条件的retry
# 重试三次之后不再重试
@retry(stop=stop_after_attempt(3))
def baidu():
res = requests.get(url = 'http://www.baidu.com')
if res.status_code == 200:
return res.text
raise Exception
web = baidu()
print(web)
重试5秒后不再重试
from tenacity import retry , stop_after_attempt , stop_after_delay
import requests
# 指定5s重试间隔
@retry(stop=stop_after_delay(5))
def baidu():
web = requests.get(url = 'http://www.baidu.com')
if web.status_code == 200:
return web.text
raise Exception
res = baidu()
print(res)
用|连接多个重试条件
from tenacity import retry , stop_after_attempt , stop_after_delay
import requests
# 使用'|' 连接多个条件组合使用
@retry(stop=stop_after_delay(5) | stop_after_attempt(3))
def baidu():
web = requests.get(url = 'http://www.baidu.com')
if web.status_code == 200:
return web.text
raise Exception
res = baidu()
print(res)
在重试前设置随机等待时间
from tenacity import retry , stop_after_attempt , stop_after_delay , wait_fixed , wait_random
import requests
# 在程序重试前设置随机等待时间
@retry(wait = wait_random(min=1,max=2))
def baidu():
web = requests.get(url = 'http://www.baidu.com')
if web.status_code == 200:
return web.text
raise Exception
res = baidu()
print(res)
按照指定数等待时间
from tenacity import retry , stop_after_attempt , stop_after_delay , wait_fixed , wait_random , wait_exponential
import requests
# 按照指定数等待时间
@retry(wait = wait_exponential(multiplier=2,min = 3,max = 100))
def baidu():
web = requests.get(url = 'http://www.baidu.com')
if web.status_code == 200:
return web.text
raise Exception
res = baidu()
print(res)
有触发条件的retry语句
from tenacity import retry ,retry_if_exception_type ,retry_if_result
import requests
# 有触发条件的retry语句
@ retry(retry = retry_if_exception_type(IOError))
def fun_1():
print('巴拉巴拉巴拉巴拉')
raise Exception
def fun_2(value):
return value is None
@ retry(retry = retry_if_exception_type(fun_2))
def fun_3():
print('滴滴滴滴滴')
@ retry(retry=(retry_if_result(fun_2)|retry_if_exception_type()))
def fun_4():
print('呼呼呼呼呼呼')
在retry前后增加log
from tenacity import retry , stop_after_attempt ,before_log ,after_log , before_sleep_log
import logging
logger = logging.getLogger(__name__)
@ retry(stop = stop_after_attempt(3),before = before_log(logger,logging.DEBUG))
def fun_1():
raise Exception('Error')
@ retry(stop = stop_after_attempt(3),after = after_log(logger,logging.DEBUG))
def fun_2():
raise Exception('Error')
@ retry(stop = stop_after_attempt(3),before_sleep = before_sleep_log(logger,logging.DEBUG))
def fun_3():
raise Exception('Error')
推荐阅读
评论