Python:httplib getresponse会发出许多recv()调用

getresponse在读取
HTML请求的标头时发出许多recv调用.它实际上为每个字节发出recv,导致许多系统调用.如何优化?

我在带有strace dump的Ubuntu机器上验证过.

示例代码:

conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD", "/index.html")
r1 = conn.getresponse()

strace dump:

sendto(3, "HEAD /index.html HTTP/1.1\r\nHost:"..., 78, 0, NULL, 0) = 78
recvfrom(3, "H", 1, 0, NULL, NULL)      = 1
recvfrom(3, "T", 1, 0, NULL, NULL)      = 1
recvfrom(3, "T", 1, 0, NULL, NULL)      = 1
recvfrom(3, "P", 1, 0, NULL, NULL)      = 1
recvfrom(3, "/", 1, 0, NULL, NULL)      = 1
...

最佳答案

r = conn.getresponse(buffering=True)

在Python 3.1上没有缓冲参数(默认情况下).

点赞