我一直在测试来自云蟒请求的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帐户对象实例化移到请求处理程序之外,以便在请求之间共享:
# -*- 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连接的数量,但是这种方法安全吗,即线程安全吗?
发布于 2015-03-14 07:52:45
请求使用urllib3作为连接池,即线程安全。因此,只要您不调用任何更改其状态的方法(或者只在您开始发出请求之前这样做),您就应该没事。
https://stackoverflow.com/questions/29046503
复制相似问题