Python: การเขียนโปรแกรม async และ pool_maxsize ด้วย HTTPAdapter

วิธีที่ถูกต้องในการใช้ HTTPAdapter กับการเขียนโปรแกรม Async และการเรียกใช้เมธอดคืออะไร คำขอทั้งหมดนี้จัดทำขึ้นในโดเมนเดียวกัน

ฉันกำลังเขียนโปรแกรมแบบอะซิงก์ใน Celery โดยใช้ eventlet และทดสอบโหลดบนไซต์ใดไซต์หนึ่งของฉัน ฉันมีวิธีการที่ฉันเรียกใช้ซึ่งทำการร้องขอไปยัง url

def get_session(url): # gets session ส่งคืนส่วนหัวของซอร์ส, proxies = header_proxy() # ตั้งค่าตัวแปรที่จำเป็นทั้งหมดของเราเป็น None เพื่อว่าในกรณีที่เกิดข้อผิดพลาด # เราจะมั่นใจได้ว่าเราจะไม่ทำลายการตอบสนอง = none status_code = none out_data = ไม่มีเนื้อหา = ไม่มี

try:
    # we are going to use request-html to be able to parse the
    # data upon the initial request

    with HTMLSession() as session:
        # you can swap out the original request session here
        # session = requests.session()
        # passing the parameters to the session
        session.mount('https://', HTTPAdapter(max_retries=0, pool_connections=250, pool_maxsize=500))
        response = session.get(url, headers=headers, proxies=proxies)
        status_code = response.status_code
        try:
            # we are checking to see if we are getting a 403 error on all requests. If so,
            # we update the status code
            code = response.html.xpath('''//*[@id="accessDenied"]/p[1]/b/text()''')
            if code:
                status_code = str(code[0][:-1])
            else:
                pass
        except Exception as error:
            pass
            # print(error)
        # assign the content to content
        content = response.content
except Exception as error:
    print(error)
    pass

หากฉันละพารามิเตอร์ pool_connections และ pool_maxsize และเรียกใช้โค้ด ฉันได้รับข้อผิดพลาดที่ระบุว่าฉันมีการเชื่อมต่อที่เปิดไม่เพียงพอ อย่างไรก็ตาม ฉันไม่ต้องการเปิดการเชื่อมต่อจำนวนมากโดยไม่จำเป็นหากฉันไม่ต้องการ


person CENTURION    schedule 18.03.2019    source แหล่งที่มา
comment
สิ่งนี้เกี่ยวข้องกับคื่นฉ่ายอย่างไร?   -  person Tomáš Linhart    schedule 18.03.2019
comment
async ทำในคื่นฉ่ายกับ eventlet Celery worker -l info -n apple --concurrency=100 --pool=eventlet   -  person CENTURION    schedule 19.03.2019
comment
อัปเดตเพื่อระบุว่าฉันใช้คื่นฉ่ายอย่างไร ขอบคุณ!   -  person CENTURION    schedule 19.03.2019


คำตอบ (1)


ตามนี้... https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/ ฉันจะเดาว่าสิ่งนี้ใช้ได้กับโฮสต์และไม่ใช่งานอะซิงก์มากนัก ดังนั้นฉันจึงตั้งค่าจำนวนสูงสุดเป็นจำนวนการเชื่อมต่อสูงสุดที่สามารถนำมาใช้ซ้ำต่อโฮสต์ได้ หากฉันเข้าถึงโดเมนหลายครั้ง การเชื่อมต่อจะถูกนำมาใช้ซ้ำ

person CENTURION    schedule 18.03.2019