【发布时间】:2020-12-26 00:11:44
【问题描述】:
我需要在一定时间内等待服务器成功响应。 最简单的方法:
def wait_for_response():
response = None
time = 60
while time > 0:
response = requests.get(url=some_url)
if response:
break
time.sleep(5)
time -= 5
if not response:
print("SOME ERROR")
return response.json()
但我不认为这个解决方案是好的。还有其他更好的解决方案吗? 请注意,程序只有在函数完成工作后才能继续工作
【问题讨论】:
-
没有。它说答案应该多长时间。但在我的情况下,重要的不是答案本身,而是它的状态。答案可能会立即出现,但他的状态可能是 500
标签: python python-3.x asynchronous async-await python-requests