繁体   English   中英

在线程中更改 Python 的“请求”模块的连接池大小

[英]Change the connection pool size for Python's "requests" module when in Threading

(编辑:也许我对这个错误的含义有误。这是否表明我的 CLIENT 的连接池已满?或者 SERVER 的连接池已满,这是我的客户端给出的错误?)

我正在尝试使用 python threadingrequests模块同时发出大量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官方文档中,包requestsurllib3使用来实际执行它的请求。 以下是您问题的相关部分,添加了我自己的一些注释并省略了代码示例,因为requests具有不同的 API:

PoolManager类根据需要自动为每个主机创建ConnectionPool实例。 默认情况下,它会保留最多 10 个 ConnectionPool 实例[注意:这是requests.adapters.HTTPAdapter()pool_connections ,它具有相同的默认值 10] 如果您向许多不同的主机发出请求,则增加此数量可能会提高性能

但是,请记住,这确实会增加内存和套接字消耗。

类似地,ConnectionPool 类保留了单个HTTPConnection实例的池。 这些连接在单个请求期间使用,并在请求完成时返回到池中。 默认情况下,只有一个连接将被保存再利用[注:这是pool_maxsizeHTTPAdapter()并请求更改默认值从1到10。 如果您同时向同一主机发出许多请求,则增加此数量可能会提高性能

ConnectionPool 的池化行为与 PoolManager 不同。 默认情况下,如果发出新请求并且池中没有空闲连接,则将创建一个新连接。 但是,如果存在超过maxsize连接,则不会保存此连接。 这意味着 maxsize 不确定可以对特定主机打开的最大连接数,而只是确定池中保留的最大连接数。 但是,如果您指定block=True [注意:在HTTPAdapter()作为pool_block可用]那么最多可以有 maxsize 个连接打开到特定主机

鉴于此,以下是您的情况:

  • 提到的所有池都是 CLIENT 池。 您(或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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM