cloudant python https 连接池?

cloudant python https connection pooling?

作为 gunicorn 请求处理的一部分,我一直在对来自 cloudant python 请求的 https 连接池进行一些测试:

# -*- coding: utf-8 -

from requests.adapters import HTTPAdapter
import cloudant
import logging
import json

# log when new connections are started by urllib3
logging.basicConfig()
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

def app(environ, start_response):

    httpAdapter = HTTPAdapter(pool_connections=10, pool_maxsize=100)

    account = cloudant.Account('education', async=False)
    account._session.adapters['https://'] = httpAdapter

    db = account.database('foundbite')

    db_response = db.get( '_all_docs' )
    data = str.encode(json.dumps(db_response.json()))
    status = str(db_response.status_code)

    response_headers = [
        ('Content-type', 'application/json'),
        ('Content-Length', str(len(data))),
    ]
    start_response(status, response_headers)
    return iter([data])

如果我发出 5 个请求,您可以看到启动了 5 个新连接:

INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None

一个选项是将 cloudant Account 对象实例移到请求处理程序之外,以便它可以在请求之间共享:

# -*- coding: utf-8 -

from requests.adapters import HTTPAdapter
import cloudant
import logging
import json

# log when new connections are started by urllib3
logging.basicConfig()
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

httpAdapter = HTTPAdapter(pool_connections=10, pool_maxsize=100)

account = cloudant.Account('education', async=False)
account._session.adapters['https://'] = httpAdapter

def app(environ, start_response):

    db = account.database('foundbite')

    db_response = db.get( '_all_docs' )
    data = str.encode(json.dumps(db_response.json()))
    status = str(db_response.status_code)

    response_headers = [
        ('Content-type', 'application/json'),
        ('Content-Length', str(len(data))),
    ]
    start_response(status, response_headers)
    return iter([data])

这次只创建一个 https 连接,用于所有 5 个请求:

INFO: requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): education.cloudant.com
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"GET /foundbite/_all_docs HTTP/1.1" 200 None

问题:第二种方法将减少昂贵的 https 连接的数量,但是这种方法安全吗,即它是线程安全的吗?

Requests 使用 urllib3 作为线程安全的连接池。因此,只要您不调用任何改变其状态的方法(或者只在您开始发出请求之前这样做),您就应该没问题。