[英]Python requests.get keeps getting timed out for about a minute, then continues working normally
I have been trying to resolve this issue for about a week now. 我已经尝试解决此问题大约一个星期了。 Either I am missing something really obvious or the problem is on the server side of the API, or server is intentionally stalling me (I coded in python). 我可能错过了确实很明显的东西,或者问题出在API的服务器端,或者服务器故意使我停滞了(我用python编码)。
What I am trying to do: 我正在尝试做的是:
I coded this in python, using requests library. 我使用请求库在python中对此进行了编码。 It works fine for a few iterations, but then server stops responding. 经过几次迭代,它仍然可以正常工作,但是服务器停止响应。 To overcome this, I added timeout to requests.get. 为了克服这个问题,我在request.get中添加了超时。 It times out, but the server does not respond to new queries either, for around 1 minute. 它超时,但是服务器在大约1分钟内也没有响应新查询。 Then everything works smoothly for a few iterations again, then things stall, and this repeats. 然后,一切又可以顺利进行几次迭代,然后停顿,然后重复一次。
Here is the python code. 这是python代码。
import requests, json
import thread, threading
from time import sleep, clock
#Get queue
conn = requests.get('http://data.bter.com/api/1/pairs/')
mainQueue = json.loads(conn.content)
conn.close()
#Variable globals
marketCount = 0
queue = mainQueue[:]
#Static globals
lock = threading.Lock()
completeSize = len(queue)
def getOrderData(marketid):
global queue, marketCount
try:
data = requests.get(str('http://data.bter.com/api/1/depth/'
+marketid), timeout = 3)
except:
with lock:
print "Timed out: %s" % marketid
queue.append(marketid)
return
with lock:
marketCount += 1
data.close()
return
while True:
print "##################################"
#Initialize data
crT = clock()
marketCount = 0
queue = mainQueue[:]
#Start retrieving all markets
while marketCount != completeSize:
while len(queue) == 0 and marketCount != completeSize:
sleep(0.01)
if marketCount != completeSize:
marketid = queue.pop(0)
thread.start_new_thread(getOrderData, (marketid,))
#Print time spent
print "Finished, total time:",clock()-crT
sleep(1)
Here is the way program behaves during runtime. 这是程序在运行时的行为方式。
Finished indicates that I got all the financial data once, and starting to update it again. “完成”表示我一次获得了所有财务数据,并开始再次对其进行更新。 As you can see, it seems that everything works fine, and then it starts stalling, timing out. 如您所见,似乎一切正常,然后开始停顿,超时。 All of a sudden, things start working normally again. 突然,事情又开始正常工作了。 I also noticed that after I close get connection with data.close(), a tcp connection with state TIME_WAIT remains in the tcp monitoring program for a long time. 我还注意到,在关闭与data.close()的连接后,状态为TIME_WAIT的tcp连接将在tcp监视程序中保留很长时间。 After a few iterations, there are TONS of them, just waiting with the TIME_WAIT state. 经过几次迭代后,其中有大量TONS,仅在TIME_WAIT状态下等待。
So, here is my question 所以,这是我的问题
I am aware that my code does not save the data, yet. 我知道我的代码还没有保存数据。 I am just trying to get it first. 我只是想先得到它。 Code is bad, but since I am testing a short snipped I did not bother with commenting (I changed it so many times trying to find a way) 代码不好,但是因为我正在测试一个简短的片段,所以我没有理会注释(我改变了很多次,试图找到一种方法)
Thanks in advance. 提前致谢。 I really hope I can overcome this. 我真的希望我能克服这个困难。
You are executing requests very frequently, which is most likely not allowed by the server. 您正在非常频繁地执行请求,这很可能是服务器不允许的。 The technique they are probably using is called throttling , have a look at http://www.django-rest-framework.org/api-guide/throttling , they have a nice explanation about it. 他们可能使用的技术称为调节 ,请访问http://www.django-rest-framework.org/api-guide/throttling ,他们对此有很好的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.