Python: httplib getresponse calls many recv () calls

getresponse many recv calls when reading the header of an HTML request. It actually produces recv for each byte, which leads to many system calls. How can it be optimized?

I checked on an Ubuntu machine with a strace dump.

code example:

 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 ... 
+4
source share
1 answer
 r = conn.getresponse(buffering=True) 

There is no buffering option in Python 3.1+ (default).

+2
source

All Articles