Python:异步编程和 pool_maxsize 使用 HTTPAdapter

Python: async programming and pool_maxsize with HTTPAdapter

将 HTTPAdapter 与异步编程和调用方法一起使用的正确方法是什么?所有这些请求都是针对同一个域发出的。

我正在使用 eventlet 在 Celery 中进行一些异步编程,并在我的一个站点上测试负载。我有一个方法,我调用它向 url 发出请求。

def get_session(url): # 获取 session returns 来源 headers, 代理 = header_proxy() # 将我们所有必要的变量设置为 None 以便在发生错误时 # 我们可以确保我们不会崩溃 回应 = None status_code = None out_data = None 内容 = None

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_connectionspool_maxsize 参数以及 运行 代码,我会收到一条错误消息,指出我没有足够的打开连接。但是,如果不需要,我不想不必要地打开大量连接。

基于此... https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/ 我猜这适用于主机而不是异步任务。因此,我将最大数量设置为每个主机可以重复使用的最大连接数。如果我多次访问一个域,连接将被重新使用。