gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:23:19 24 4
gpt4 key购买 nike

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

我在 Ubuntu 机器上用 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 参数(这是默认值)。

关于Python:httplib getresponse 发出许多 recv() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14519829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com