cloudant python https连接池?

时间:2015-03-14 07:18:05

标签: python couchdb gunicorn cloudant

作为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连接数量,但这种方法是否安全,即线程安全吗?

1 个答案:

答案 0 :(得分:1)

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