注:このソリューションは、接続プールの構築を制御できない場合にのみ使用してください (@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