问题描述
我正在使用 python 的请求模块进行 HTTP 通信,我想知道如何重用已经建立的 TCP 连接?requests 模块是无状态的,如果我对同一个 URL 重复调用 get,它不是每次都会创建一个新连接吗?
I am working with python's requests module for HTTP communication, and I am wondering how to reuse already-established TCP connections? The requests module is stateless and if I repeatedly call get for the same URL, wouldn't it create a new connection each time?
谢谢!!
推荐答案
requests
模块不是无状态的;它只是让您忽略状态并有效地使用全局单例状态(如果您选择这样做).*
The requests
module is not stateless; it just lets you ignore the state and effectively use a global singleton state if you choose to do so.*
而且它(或者,更确切地说,底层库之一,urllib3
)维护一个由(主机名,端口)对键控的连接池,因此如果可以,它通常会神奇地重用连接.
And it (or, rather, one of the underlying libraries, urllib3
) maintains a connection pool keyed by (hostname, port) pair, so it will usually just magically reuse a connection if it can.
正如文档所说:
好消息——多亏了 urllib3,keep-alive 是 100% 自动的在一个会话中!您在会话中提出的任何请求都将自动重用合适的连接!
请注意,连接只会释放回池以供重用一旦读取了所有主体数据;确保将 stream
设置为False
或读取 Response
对象的 content
属性.
Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set stream
to False
or read the content
property of the Response
object.
那么,如果可以的话"是什么意思?正如上面的文档所暗示的那样,如果您保持流式响应对象处于活动状态,则它们的连接显然无法重用.
So, what does "if it can" mean? As the docs above imply, if you're keeping streaming response objects alive, their connections obviously can't be reused.
此外,连接池实际上是一个有限的缓存,而不是无限的,因此如果您发送大量连接并且其中两个连接到同一台服务器,您将不会总是重用连接,只是经常.但通常,这就是您真正想要的.
Also, the connection pool is really a finite cache, not infinite, so if you spam out a ton of connections and two of them are to the same server, you won't always reuse the connection, just often. But usually, that's what you actually want.
这篇关于python请求模块和连接重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!