sqlalchemy
app = tornado.web.Application(
...
db=SQLAlchemy(url=get_db_url(),
engine_options={
"pool_pre_ping": True,
"execution_options": {
"isolation_level": "AUTOCOMMIT"
}
}
)
所有问题都消失了,但是现在我面临着一个不同的问题。
假设其中一个网页正在后端运行重查询,我像这样运行:
from tornado_sqlalchemy import SessionMixin
class MyPage(tornado.web.RequestHandler, SessionMixin):
# Run things in the background
executor = ThreadPoolExecutor(max_workers=10)
def render_myhtml(self):
# self.session generate a unique session for this class by SessionMixin automatically
results = self.session.query(...)
我打开网站-12次(= 12个选项卡)以检查其处理负载的方式。
结果是:
@run_on_executor
def get_query_results(self):
...
500错误,后端显示出:
X tabs - works fine ( the number of X changes )
12 - x tabs - result with 500 internal server error.
现在:
# One tab received and crashed, which started a chain of other errors
pymysql.err.InterfaceError: (0, '')
# Other tab afterwards received and crashed:
AttributeError: 'NoneType' object has no attribute 'read'
# Other tab afterwards received and crashed:
pymysql.err.InternalError: Packet sequence number wrong - got 102 expected 3
# Other tab received:
sqlalchemy.exc.PendingRollbackError: Can't reconnect until invalid transaction is rolled back. (Background on this error at: https://sqlalche.me/e/14/8s2b)
我的问题是 - 龙卷风遇到错误后,它不会“发布”或恢复这些会话。 我将尝试从现在开始加载的任何页面,直到重新启动龙卷风服务器 - 只需挂起直到产生的500个内部错误。要复制,我们可以在
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/14/3o7r)
中设置以下内容
engine_options
一次打开3个标签。
任何关于如何解决这个问题并恢复这些会议或将其丢弃的想法?
您应该在每个用户的请求中打开会话(并关闭)
您可以增加满足。但是只有当您的数据库允许
,我建议您在下一步使用Sqlalchemy
包装。
发动机:
pool_size = 2
max_overflow = 0
pool_size
max_overflow:增加max_overflow将使您的龙卷风(pool_size + max_overflow)在开销场景中。 expire_on_commit:将expire_on_commit设置为true。查询结束后,这将立即关闭连接
pool_recycle:设置pool_recylce = n值,以每n秒后重置池connection_timeout:设置正确的超时
我的龙卷风服务器使用上下文管理器建立连接,我可以根据需要轻松覆盖。就您而言,似乎在发生错误后没有恢复回滚。在我的龙卷风服务器中,我在上下文管理器中手动进行操作。这是我的上下文经理:
engine = asa.create_async_engine(
self.app_context.db_config.db_async_con_string,
echo=False,
pool_pre_ping=True,
pool_size=self.app_context.db_config.pool_size,
max_overflow=self.app_context.db_config.max_overflow,
)
DEFSession_scope():
session = nonth