Python Sql Alchemy队列池限制溢出
我有一个Sql Alchemy应用程序正在返回超时: TimeoutError:已达到大小为5的队列池限制溢出10, 连接超时,超时30 我在另一篇文章中读到,当我不关闭会话时会发生这种情况,但我不知道这是否适用于我的代码: 我连接到init.py中的数据库:Python Sql Alchemy队列池限制溢出,python,session,sqlalchemy,zope,connection-timeout,Python,Session,Sqlalchemy,Zope,Connection Timeout,我有一个Sql Alchemy应用程序正在返回超时: TimeoutError:已达到大小为5的队列池限制溢出10, 连接超时,超时30 我在另一篇文章中读到,当我不关闭会话时会发生这种情况,但我不知道这是否适用于我的代码: 我连接到init.py中的数据库: from .dbmodels import ( DBSession, Base, engine = create_engine("mysql://" + loadConfigVar("user") + ":" +
from .dbmodels import (
DBSession,
Base,
engine = create_engine("mysql://" + loadConfigVar("user") + ":" + loadConfigVar("password") + "@" + loadConfigVar("host") + "/" + loadConfigVar("schema"))
#Sets the engine to the session and the Base model class
DBSession.configure(bind=engine)
Base.metadata.bind = engine
然后在另一个python文件中,我在两个函数中收集一些数据,但使用我在init.py中初始化的DBSession:
from .dbmodels import DBSession
from .dbmodels import resourcestatsModel
def getFeaturedGroups(max = 1):
try:
#Get the number of download per resource
transaction.commit()
rescount = DBSession.connection().execute("select resource_id,count(resource_id) as total FROM resourcestats")
#Move the data to an array
resources = []
data = {}
for row in rescount:
data["resource_id"] = row.resource_id
data["total"] = row.total
resources.append(data)
#Get the list of groups
group_list = toolkit.get_action('group_list')({}, {})
for group in group_list:
#Get the details of each group
group_info = toolkit.get_action('group_show')({}, {'id': group})
#Count the features of the group
addFesturedCount(resources,group,group_info)
#Order the FeaturedGroups by total
FeaturedGroups.sort(key=lambda x: x["total"],reverse=True)
print FeaturedGroups
#Move the data of the group to the result array.
result = []
count = 0
for group in FeaturedGroups:
group_info = toolkit.get_action('group_show')({}, {'id': group["group_id"]})
result.append(group_info)
count = count +1
if count == max:
break
return result
except:
return []
def getResourceStats(resourceID):
transaction.commit()
return DBSession.query(resourcestatsModel).filter_by(resource_id = resourceID).count()
会话变量的创建方式如下:
#Basic SQLAlchemy types
from sqlalchemy import (
Column,
Text,
DateTime,
Integer,
ForeignKey
)
# Use SQLAlchemy declarative type
from sqlalchemy.ext.declarative import declarative_base
#
from sqlalchemy.orm import (
scoped_session,
sessionmaker,
)
#Use Zope' sqlalchemy transaction manager
from zope.sqlalchemy import ZopeTransactionExtension
#Main plugin session
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
因为会话是在init.py和后续代码中创建的,所以我只使用它;我需要在哪一点结束会话?或者我还需要做什么来管理池大小?您可以通过在函数
创建引擎中添加参数pool\u size和max\u overflow来管理池大小
engine = create_engine("mysql://" + loadConfigVar("user") + ":" + loadConfigVar("password") + "@" + loadConfigVar("host") + "/" + loadConfigVar("schema"),
pool_size=20, max_overflow=0)
参考是
您不需要关闭会话,但应在事务完成后关闭连接。
替换:
作者:
参考是
另外,请注意,mysql已经过时的连接在特定时间段后关闭(这个时间段可以在mysql中配置,我不记得默认值),因此您需要将pool_recycle值传递到引擎创建中将以下方法添加到代码中。它将自动关闭所有未使用/挂起的连接,并防止代码出现瓶颈。特别是当您使用以下语法Model.query.filter_by(attribute=var).first()和relationships/lazy加载时
@app.teardown_appcontext
def shutdown_session(exception=None):
db.session.remove()
此处提供了相关文档:您也可以在def末尾使用方法。
这会完全关闭所有当前签入的数据库连接。在第二个代码段中,哪里定义了transaction
?哪里使用了getResourceStats
?看起来有一半的代码丢失了-您可以全部添加吗?或者可能无法诊断问题。此代码是否按原样工作?除了@TomDalton提到的问题外,import
语句看起来非常奇怪。错误发生在什么时候?这是第一个吗?瞧,你能完全(或显著)减少这一点,并且仍然表现出相同的行为吗?这里似乎有很多业务逻辑,而这些逻辑不太可能是问题的原因。尝试删除它,并发布一个完整的工作(语法正确)示例,我很乐意提供帮助。MySQL中非活动会话的默认超时是8小时,用于交互式和非交互式连接:[1][2]您提到我们应该关闭连接,而不是会话。但fastAPI示例说我们应该关闭会话。不是请求上下文?
connection = DBSession.connection()
try:
rescount = connection.execute("select resource_id,count(resource_id) as total FROM resourcestats")
#do something
finally:
connection.close()
@app.teardown_appcontext
def shutdown_session(exception=None):
db.session.remove()