2013-08-27 4 views
32

(संपादित करें: शायद मैं इस त्रुटि का अर्थ में गलत हूं। क्या यह इंगित करता है कि मेरे क्लाइंट पर कनेक्शन पूल भरा हुआ है या सर्वर पर कनेक्शन पूल भरा हुआ है और यह मेरा क्लाइंट दिया जा रहा त्रुटि है?)क्या मैं पाइथन के "अनुरोध" मॉड्यूल के लिए कनेक्शन पूल आकार बदल सकता हूं?

मैं http अनुरोध समवर्ती अजगर threading और requests मॉड्यूल का उपयोग कर की एक बड़ी संख्या बनाने के लिए प्रयास कर रहा हूँ।

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

क्या मैं अनुरोधों के लिए कनेक्शन पूल का आकार बढ़ाने के लिए कर सकते हैं: मैं लॉग में इस त्रुटि देख रहा हूँ?

उत्तर

70

इस चाल करना चाहिए:

import requests 
sess = requests.Session() 
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100) 
sess.mount('http://', adapter) 
resp = sess.get("/mypage") 
+5

यह मेरे लिए काम करता है। इसे सही उत्तर के रूप में चिह्नित किया जाना चाहिए। – reish

9

नोट: इस समाधान केवल यदि आप कनेक्शन पूल का निर्माण कार्य (@ 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 
+1

अनुरोधों में कनेक्शन पुल कन्स्ट्रक्टर पैराम्स की आपूर्ति के लिए अंतर्निहित एपीआई है, कन्स्ट्रक्टर को पैच करना अनावश्यक है। (@ जहांजा का जवाब देखें।) – shazow

+1

यह संदर्भ पर निर्भर करता है। यदि आपके पास HTTPAdapter बनाने पर नियंत्रण है, तो कन्स्ट्रक्टर का उपयोग करना सही समाधान है। लेकिन ऐसे मामले हैं जहां कनेक्शन पूल को कुछ ढांचे या पुस्तकालय में कहीं गहराई से दफनाया जाता है। उन मामलों में आप लाइब्रेरी को पैच कर सकते हैं या कनेक्शन पूल कन्स्ट्रक्टर को पैच कर सकते हैं जैसा मैंने ऊपर वर्णित किया है। –

+0

मैंने अपने समाधान में एक स्पष्टीकरण जोड़ा। –

संबंधित मुद्दे