Uwaga: korzystać z tego rozwiązania tylko wtedy, gdy nie można kontrolować budowę puli połączeń (jak opisano w odpowiedzi Jahaja za @).
Problem polega na tym, że urllib3
tworzy pule na żądanie. Wywołuje konstruktora klasy urllib3.connectionpool.HTTPConnectionPool
bez parametrów. Zajęcia są rejestrowane w urllib3 .poolmanager.pool_classes_by_scheme
. Sztuką jest zastąpienie klas klasami o różnych domyślnych parametrach:
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
Następnie możesz zadzwonić, aby ustawić nowe domyślne parametry. Upewnij się, że jest to wywoływane przed wykonaniem jakiegokolwiek połączenia.
patch_http_connection_pool(maxsize=16)
Jeśli używasz połączenia https można utworzyć podobną funkcję:
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
Działa to dla mnie. Powinien być oznaczony jako poprawna odpowiedź. – reish