python - SQLAlchemy / gevent / cx_Oracle 池大小保持在 1
问题描述
尽管我使用的池大小为 50,但我无法让 SQLAlchemy v. 1.3.22 与 gevent 21.1.2 和 cx_Oracle 8.1.0 一次发出多个查询。
我不确定哪个库更重要,因此我添加了所有似乎适用的标签。
我确认我的用户可以与 Oracle 数据库建立多个连接 - 我有另一个程序,我可以使用相同的凭据和数据库轻松地并行运行 50 个查询。
我使用 siege 调用下面的代码——无论我设置什么并发,我总是在池中获得一个连接。
一旦我从“应用程序”处理程序中删除与 SQLAlchemy 相关的代码,siege 就会报告预期的 50 并发,我理解这意味着它实际上一次运行那么多并发连接。
我知道 SQLAlchemy 会按需建立新的连接,但我不明白为什么它没有在这里这样做,因为显然有需求。
from gevent.monkey import patch_all
patch_all()
from gevent.pywsgi import WSGIServer
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
username = '<hidden>'
password = '<hidden>'
host = '<hidden>'
port = '<hidden>'
database = '<hidden>'
url = 'oracle://{}:{}@{}:{}/{}'.format(username, password, host, port, database)
engine = create_engine(url, pool_size=50)
Session = sessionmaker(bind=engine)
def application(env, start_response):
session = Session()
result = session.execute('select 1+1 from dual')
result.fetchall()
session.close()
print('Status:', engine.pool.status())
start_response('200 OK', [('Content-Type', 'text/html')])
return [b'<b>hello world</b>']
if __name__ == '__main__':
print('Serving on 8088...')
WSGIServer(('127.0.0.1', 8088), application, log=None).serve_forever()
围城:
siege -r 500 -c 50 http://localhost:8088/
Transactions: 31 hits
Availability: 100.00 %
Elapsed time: 4.13 secs
Data transferred: 0.00 MB
Response time: 2.60 secs
Transaction rate: 7.51 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 19.50
Successful transactions: 31
Failed transactions: 0
Longest transaction: 4.10
Shortest transaction: 0.00
服务器的输出:
Status: Pool size: 50 Connections in pool: 1 Current Overflow: -49 Current Checked out connections: 0
Status: Pool size: 50 Connections in pool: 1 Current Overflow: -49 Current Checked out connections: 0
Status: Pool size: 50 Connections in pool: 1 Current Overflow: -49 Current Checked out connections: 0
非常感谢!
解决方案
在顶部添加
import greenify
greenify.greenify()
assert greenify.patch_lib("/usr/lib/oracle/19.5/client64/lib/libclntsh.so.19.1")
但是请调整池大小,否则您的应用程序将面临被严重杀死的风险。您需要先对其进行测试,该问题提供了一个出色的脚本。见https://github.com/oracle/python-cx_Oracle/issues/126
推荐阅读
- android - 解析json对象后如何传递数据
- c# - C# 为什么我们不能将委托匿名方法分配给 var 关键字
- python - 如何进行 3d 数组的映射
- python - 使用 pandas xlsxwriter 写入 excel 表中的多行
- captcha - 是否有绕过验证码验证访问网站的解决方案?
- node.js - 有没有办法让我的 API 只能由我的 node.js 服务器查询?
- java - Elasticsearch api - @timestamp 的订单聚合
- javascript - 将变量分配给值发出Javascript
- node.js - 有没有办法将二进制文件输入(流?)直接提供给 multer 中间件(用于单元测试目的)?
- swift - Swift 中的单位和度量