我正在使用Python请求发帖请求 我正在尝试做这样的事情,如下面的帖子所示: Retry with requests
当出现连接错误或收到的响应状态代码来自status_forcelist时,它应该重试(工作正常)。我想要做的是在第一次尝试之后(重试之前),我想做一些其他的事情。如果我可以捕获异常并处理它来做其他事情,这是可能的。但似乎请求在连接错误或响应代码在status_forcelist中时不会引发任何异常,除非重试计数达到最大配置。我怎样才能做到这一点?
以下是代码示例:
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def do_something_more():
## do something to tell user API failed and it will retry
print("I am doing something more...")
...用法
t0 = time.time()
try:
response = requests_retry_session().get(
'http://localhost:9999',
)
except Exception as x:
# Catch exception when connection error or 500 on first attempt and do something more
do_somthing_more()
print('It failed :(', x.__class__.__name__)
else:
print('It eventually worked', response.status_code)
finally:
t1 = time.time()
print('Took', t1 - t0, 'seconds')
我知道在允许的最大尝试次数(在retries = 3中定义)之后会引发异常。我想要的只是来自请求或urllib3的一些信号,告诉我的主程序第一次尝试失败,现在它将开始重试。所以我的程序可以基于它做更多的事情。如果没有通过例外,还有别的东西。
答案 0 :(得分:0)
最强大的方式(但可能不是最好的,当然也不是最有效的)只是将retries
设置为0 - 然后每次都会引发异常。然后我会用手动计数器调用该函数三次,这将计算你尝试重新连接的次数。这样的事情(我没有检查它是否有效,只是想告诉你我的思维方式):
counter = 0
t0 = time.time()
for i in range(3):
try:
response = requests_retry_session().get(
'http://localhost:9999',
)
#This should already be set to retries=0
except MaxRetryError:
counter += 1
do_something_more()
print('It is the {} time it failed'.format(counter))
else:
break #If there isn't MaxRetryError, it connected successfully, so we don't have to execute for anymore
t1 = time.time()
print('Took', t1 - t0, 'seconds')