Nota: Utilizzare questa soluzione solo se non è possibile controllare la costruzione del pool di connessioni (come descritto nel @ risposta di Jahaja).
Il problema è che il urllib3
crea i pool su richiesta. Chiama il costruttore della classe urllib3.connectionpool.HTTPConnectionPool
senza parametri. Le classi sono registrate in urllib3 .poolmanager.pool_classes_by_scheme
. Il trucco è quello di sostituire le classi con i tuoi classi che hanno diversi parametri di default:
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
quindi è possibile chiamare per fissare nuovi parametri di default. Assicurarsi che questo venga chiamato prima che venga effettuata qualsiasi connessione.
patch_http_connection_pool(maxsize=16)
Se si utilizzano connessioni HTTPS è possibile creare una funzione simile:
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
fonte
2014-03-07 15:06:21
Questo funziona per me. Dovrebbe essere contrassegnato come la risposta corretta. – reish