import logging
import time
from functools import wraps
from traceback import print_exc
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('SPIDER')
class Spider:
def retry(*args):
"""
重试装饰器
支持指定重试次数
"""
if args:
num = args[0]
else:
num = 1
def recive_func(func):
@wraps(func)
def wrapper(*args, **kwargs):
# print()
retry_num = 0
success = False
res = None
while True:
try:
res = func(*args, **kwargs)
success = True
break
except:
logger.error(f'{func.__name__}网络请求失败,重试...')
print_exc()
retry_num += 1
time.sleep(10)
print(num)
return res
return wrapper
return recive_func
@retry()
def first_req(self, job):
"""
初次请求
"""
print(job)
job+1
if __name__ == '__main__':
Spider().first_req('dd')
网友评论