Python:httplib getresponse发出许多recv()调用 [英] Python: httplib getresponse issues many recv() calls

查看:86
本文介绍了Python:httplib getresponse发出许多recv()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

getresponse issues many recv calls while reading header of an HTML request. It actually issues recv for each byte which results in many system calls. How can it be optimized?

我在具有strace dump的Ubuntu计算机上进行了验证.

I verified on an Ubuntu machine with strace dump.

示例代码:

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

跟踪转储:

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+上,没有buffering参数(默认设置).

On Python 3.1+ there is no buffering parameter (it is default).

这篇关于Python:httplib getresponse发出许多recv()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆