我正在开发一个 API 实现,我希望有一个从微服务获取响应的时间限制,如果在时间限制内没有收到响应,那么再重试一段时间(比如 3 次)< /p>
import requests
url = 'http://example.com:8000/demo'
payload = {'data1': 1, 'data2': 2}
try:
r = requests.post(url, data=payload)
except:
return # if the requests.post does not give response in a specific time-limit then I would like to retry request 3 times
python 中是否有内置函数可以让我拥有 timeout
和 retry the request for # times
?
答案 0 :(得分:1)
啊,你在算法部分做得很好。
很确定您可以自己构建循环。我喜欢在你的情况下使用 range()
。
在超时部分,请求确实支持您:
https://requests.readthedocs.io/en/master/user/quickstart/#timeouts