62

(編集: おそらく、このエラーの意味が間違っています。これは、CLIENT の接続プールがいっぱいであることを示していますか? または、SERVER の接続プールがいっぱいで、これがクライアントに与えられているエラーですか?)

Pythonとモジュールを使用して、同時に多数のhttpリクエストを作成しようとしています。ログに次のエラーが表示されます。threadingrequests

WARNING:requests.packages.urllib3.connectionpool:HttpConnectionPool is full, discarding connection:

リクエストの接続プールのサイズを増やすにはどうすればよいですか?

4

4 に答える 4

128

これでうまくいくはずです:

import requests.adapters

session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
session.mount('http://', adapter)
response = session.get("/mypage")
于 2013-09-17T09:21:42.190 に答える
29

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