python请求:重试直到收到有效响应 [英] python requests: Retrying until a valid response is received
问题描述
我想知道是否存在一种通用的模式来重试请求一定次数(可能由于服务器错误或网络故障而失败)。我想出了这个,我愿意在那里找到更好的实现。
I am wondering if there is a common pattern for retrying requests a certain number of times (which might be failing because of server error, or bad network). I came up with this and I'm willing to find better implementations out there.
cnt=0
while cnt < 3:
try:
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return json.loads(response.text)
except requests.exceptions.RequestException:
continue
cnt += 1
return False
推荐答案
您可能要考虑在重试之间引入等待,因为许多暂时性问题可能需要花费几秒钟才能清除。另外,我建议在几何上增加等待时间,以便有足够的时间使系统恢复:
You might want to consider introducing a wait between retries as a lot of transient problem might take more than a few seconds to clear. In addition, I would recommend a geometrical increase in wait time to give enough time for system to recover:
import time
cnt=0
max_retry=3
while cnt < max_retry:
try:
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return json.loads(response.text)
else:
# Raise a custom exception
except requests.exceptions.RequestException as e:
time.sleep(2**cnt)
cnt += 1
if cnt >= max_retry:
raise e
在这种情况下,请重试将在1、2和4秒后发生。只要注意最大的重试次数。您将重试次数增加到10,然后知道代码将等待17分钟。
In this case, your retries will happen after 1, 2 and 4 seconds. Just watch out for max number of retries. You increase the retry to 10 and next thing you know the code would be waiting for 17 mins.
编辑:
仔细查看您的代码,当您用尽重试后,返回 false
并没有什么意义。您确实应该向调用者提出异常,以便可以传达问题。另外,您检查是否有 requests.codes.ok
,但没有定义 else
动作。
Taking a closer look at your code, it doesn't really make sense to return false
when you exhausted retried. You should really be raising the exception to the caller so that problem can be communicated. Also, you check for requests.codes.ok
but no else
action define.
这篇关于python请求:重试直到收到有效响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!