我已经看过几个关于使用timeout参数处理挂起请求的相关帖子,但我不确定如何诊断我的请求是否挂起或是否存在其他潜在问题。我正在传输一些数据并将其记录下来并将其打印到控制台。现在两次我看到没有数据打印到控制台的延长期间,当我中断执行时,我得到以下堆栈跟踪
File "/home/matthew/Dropbox/pylibs/oandapy/oandapy.py", line 276, in start
for line in response.iter_lines(90):
File "/home/matthew/anaconda/lib/python2.7/site-packages/requests/models.py", line 663, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "/home/matthew/anaconda/lib/python2.7/site-packages/requests/models.py", line 627, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "/home/matthew/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 240, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/home/matthew/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 187, in read
data = self._fp.read(amt)
File "/home/matthew/anaconda/lib/python2.7/httplib.py", line 543, in read
return self._read_chunked(amt)
File "/home/matthew/anaconda/lib/python2.7/httplib.py", line 585, in _read_chunked
line = self.fp.readline(_MAXLINE + 1)
File "/home/matthew/anaconda/lib/python2.7/socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
File "/home/matthew/anaconda/lib/python2.7/ssl.py", line 246, in recv
return self.read(buflen)
File "/home/matthew/anaconda/lib/python2.7/ssl.py", line 165, in read
return self._sslobj.read(len)
我是否可以设置一些额外的参数或在某处记录与连接相关的更多信息,我不清楚堆栈跟踪中的问题是什么。
答案 0 :(得分:1)
您可以在创建请求对象或发送请求时为HTTP会话设置超时;见here:
timeout - (可选)Float描述请求的超时时间 秒。
然而,在传输过程中看起来有些不对劲。出于测试目的,您可以将 chunk_size = 1 传递给 iter_lines ,并立即打印结果。服务器可能无法发送HTTP分块终结符 \ r \ n (检查您是否拥有自己的服务器?值得通过https尝试其他服务器)。