我一直在对 cloudant python 的 https 连接池进行一些测试。作为gunicorn请求处理一部分的请求:
# -*- 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 进行连接池,这是线程安全的。因此,只要您不调用任何改变其状态的方法(或者仅在开始发出请求之前这样做),您应该没问题。
关于python - cloudant python https 连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046503/