编程语言
首页 > 编程语言> > Python:httplib getresponse会发出许多recv()调用

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上没有缓冲参数(默认情况下).

标签:python,python-2-7,httplib
来源: https://codeday.me/bug/20190703/1371784.html