编程语言
首页 > 编程语言> > cloudant python https连接池?

cloudant python https连接池?

作者:互联网

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

解决方法:

请求使用urllib3进行连接缓冲,这是线程安全的.因此,只要您不在帐户上调用任何会更改其状态的方法(或仅在开始发出请求之前这样做),就可以了.

标签:python,couchdb,gunicorn,cloudant
来源: https://codeday.me/bug/20191014/1912661.html