[英]Change the connection pool size for Python's "requests" module when in Threading
(编辑:也许我对这个错误的含义有误。这是否表明我的 CLIENT 的连接池已满?或者 SERVER 的连接池已满,这是我的客户端给出的错误?)
我正在尝试使用 python threading
和requests
模块同时发出大量http
请求。 我在日志中看到此错误:
WARNING:requests.packages.urllib3.connectionpool:HttpConnectionPool is full, discarding connection:
我可以做些什么来增加请求的连接池的大小?
这应该可以解决问题:
import requests.adapters
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
session.mount('http://', adapter)
response = session.get("/mypage")
注意:仅当您无法控制连接池的构造时才使用此解决方案(如@Jahaja 的回答中所述)。
问题是urllib3
创建池。 它不带参数调用urllib3.connectionpool.HTTPConnectionPool
类的构造函数。 这些类在urllib3 .poolmanager.pool_classes_by_scheme
中注册。 诀窍是用具有不同默认参数的类替换类:
def patch_http_connection_pool(**constructor_kwargs):
"""
This allows to override the default parameters of the
HTTPConnectionPool constructor.
For example, to increase the poolsize to fix problems
with "HttpConnectionPool is full, discarding connection"
call this function with maxsize=16 (or whatever size
you want to give to the connection pool)
"""
from urllib3 import connectionpool, poolmanager
class MyHTTPConnectionPool(connectionpool.HTTPConnectionPool):
def __init__(self, *args,**kwargs):
kwargs.update(constructor_kwargs)
super(MyHTTPConnectionPool, self).__init__(*args,**kwargs)
poolmanager.pool_classes_by_scheme['http'] = MyHTTPConnectionPool
然后你可以调用来设置新的默认参数。 确保在建立任何连接之前调用它。
patch_http_connection_pool(maxsize=16)
如果您使用 https 连接,您可以创建一个类似的功能:
def patch_https_connection_pool(**constructor_kwargs):
"""
This allows to override the default parameters of the
HTTPConnectionPool constructor.
For example, to increase the poolsize to fix problems
with "HttpSConnectionPool is full, discarding connection"
call this function with maxsize=16 (or whatever size
you want to give to the connection pool)
"""
from urllib3 import connectionpool, poolmanager
class MyHTTPSConnectionPool(connectionpool.HTTPSConnectionPool):
def __init__(self, *args,**kwargs):
kwargs.update(constructor_kwargs)
super(MyHTTPSConnectionPool, self).__init__(*args,**kwargs)
poolmanager.pool_classes_by_scheme['https'] = MyHTTPSConnectionPool
Jahaja 的回答已经为您的问题提供了推荐的解决方案,但它没有回答发生了什么,或者如您所问,此错误意味着什么。
一些非常详细的信息在urllib3
官方文档中,包requests
在urllib3
使用来实际执行它的请求。 以下是您问题的相关部分,添加了我自己的一些注释并省略了代码示例,因为requests
具有不同的 API:
PoolManager
类根据需要自动为每个主机创建ConnectionPool
实例。 默认情况下,它会保留最多 10 个 ConnectionPool 实例[注意:这是requests.adapters.HTTPAdapter()
的pool_connections
,它具有相同的默认值 10] 。 如果您向许多不同的主机发出请求,则增加此数量可能会提高性能但是,请记住,这确实会增加内存和套接字消耗。
类似地,ConnectionPool 类保留了单个
HTTPConnection
实例的池。 这些连接在单个请求期间使用,并在请求完成时返回到池中。 默认情况下,只有一个连接将被保存再利用[注:这是pool_maxsize
在HTTPAdapter()
并请求更改默认值从1到10。 如果您同时向同一主机发出许多请求,则增加此数量可能会提高性能ConnectionPool 的池化行为与 PoolManager 不同。 默认情况下,如果发出新请求并且池中没有空闲连接,则将创建一个新连接。 但是,如果存在超过
maxsize
连接,则不会保存此连接。 这意味着 maxsize 不确定可以对特定主机打开的最大连接数,而只是确定池中保留的最大连接数。 但是,如果您指定block=True
[注意:在HTTPAdapter()
作为pool_block
可用]那么最多可以有 maxsize 个连接打开到特定主机
鉴于此,以下是您的情况:
requests
)无法控制任何服务器连接池HttpConnectionPool
,即同时连接到同一主机的数量,因此您可以增加pool_maxsize
以匹配您用来消除警告的工作线程/线程数。pool_maxsize
是多少, requests
已经打开了您要求的尽可能多的同时连接。 如果您有 100 个线程,它将打开 100 个连接。 但是在默认值下,只有 10 个会保留在池中供以后重用,而 90 个在完成请求后将被丢弃。pool_maxsize
通过重用连接而不是通过增加并发来提高单个主机的性能。pool_connections
。 默认值已经是 10,所以如果你的所有请求都发送到同一个目标主机,增加它不会对性能产生任何影响(但它会增加使用的资源,如上述文档中所述)如果有人需要用 Python Zeep 来做这件事,并且想花一点时间来弄清楚这里是一个快速的方法:
from zeep import Client
from requests import adapters as request_adapters
soap = "http://example.com/BLA/sdwl.wsdl"
wsdl_path = "http://example.com/PATH/TO_WSLD?wsdl"
bind = "Binding"
client = Client(wsdl_path) # Create Client
# switch adapter
session = client.transport.session
adapter = request_adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10)
# mount adapter
session.mount('https://', adapter)
binding = '{%s}%s' % (soap, bind)
# Create Service
service = client.create_service(binding, wsdl_path.split('?')[0])
基本上应该在创建服务之前创建连接
答案实际上是从一个已关闭问题的 python-zeep Repo 中获取的,作为参考,我将添加它 -->这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.