Примечание. Используйте это решение, только если вы не можете контролировать создание пула соединений (как описано в ответе @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
person
Michael_Scharf
schedule
07.03.2014