注:接続プールの建設を制御できない場合にのみは、このソリューションを使用する(@ 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
これは私の作品を。正解とマークする必要があります。 – reish