requests.adapters.HTTPAdapter 中的 pool_connections 是什么意思? [英] What's the meaning of pool_connections in requests.adapters.HTTPAdapter?

查看:45
本文介绍了requests.adapters.HTTPAdapter 中的 pool_connections 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初始化请求的Session时,两个HTTPAdapter 将被创建和 挂载到 httphttps.

When initializing a requests' Session, two HTTPAdapter will be created and mount to http and https.

HTTPAdapter 是这样定义的:

class requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10,
                                    max_retries=0, pool_block=False)

虽然我理解 pool_maxsize(池可以保存的会话数)的含义,但我不明白 pool_connections 的含义或它的作用.医生说:

While I understand the meaning of pool_maxsize(which is the number of session a pool can save), I don't understand what pool_connections means or what it does. Doc says:

Parameters: 
pool_connections – The number of urllib3 connection pools to cache.

但是缓存"是什么意思?使用多个连接池有什么意义?

But what does it mean "to cache"? And what's the point using multiple connection pools?

推荐答案

Requests 使用 urllib3管理其连接和其他功能.

Requests uses urllib3 to manage its connections and other features.

重用连接是保持重复性 HTTP 请求性能的重要因素.urllib3 README 解释:

Re-using connections is an important factor in keeping recurring HTTP requests performant. The urllib3 README explains:

为什么我要重复使用连接?

性能.当您通常进行 urllib 调用时,会为每个请求创建一个单独的套接字连接.通过重用现有的套接字(从 HTTP 1.1 开始支持),请求将在服务器端占用更少的资源,并在客户端提供更快的响应时间.[...]

Performance. When you normally do a urllib call, a separate socket connection is created with each request. By reusing existing sockets (supported since HTTP 1.1), the requests will take up less resources on the server's end, and also provide a faster response time at the client's end. [...]

要回答您的问题,pool_maxsize"是每个主机要保留的连接数(这对多线程应用程序很有用),而pool_connections"是要保留的主机池数.例如,如果您要连接到 100 个不同的主机,并且 pool_connections=10,那么只会重新使用最新的 10 个主机的连接.

To answer your question, "pool_maxsize" is the number of connections to keep around per host (this is useful for multi-threaded applications), whereas "pool_connections" is the number of host-pools to keep around. For example, if you're connecting to 100 different hosts, and pool_connections=10, then only the latest 10 hosts' connections will be re-used.

这篇关于requests.adapters.HTTPAdapter 中的 pool_connections 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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