我有一个Sql Alchemy应用程序,它返回了TimeOut:
TimeoutError: 队列池大小限制为5,溢出10已达到,连接超时,超时时间30
我在不同的帖子中阅读到,当我没有关闭会话时,就会发生这种情况,但我不知道是否适用于我的代码:
我在init.py中连接到数据库:
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()))
由于会话(session)是在init.py中创建的,而在后续代码中我只是使用它;那么我需要在什么时候关闭会话呢?或者说我还需要做什么来管理池的大小呢?
transaction
定义在哪里? - Tom DaltongetResourceStats
函数在哪里被使用了?看起来代码的一半都不见了,你能否把它全部添加进去,否则可能无法诊断问题。 - Tom Daltonimport
语句看起来非常奇怪,除了@TomDalton提到的问题之外。错误是在什么时候发生的?这是最小完整可验证示例吗?也就是说,您能否将其减少(或显著减少)并仍然展示相同的行为?似乎有很多业务逻辑在其中,这很不可能是问题的原因。尝试删除它,并发布一个完整的工作(语法正确)的示例,我很乐意帮助。 - cod3monk3y