getresponse
recv
在读取 HTML 请求的标头时发出许多调用。它实际上是recv
针对每个字节发出的,这会导致许多系统调用。如何优化?
我在带有 strace 转储的 Ubuntu 机器上进行了验证。
示例代码:
conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD", "/index.html")
r1 = conn.getresponse()
strace 转储:
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
...