玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容

Flask运行报错误不理解

代码只有一行

topic = Topic.query.filter_by(topic_domain=topic_domain).first()

刷新10次就可能有一次报下面的错误:

Traceback (most recent call last): File
"/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1836, in call
return self.wsgi_app(environ, start_response) File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1820, in wsgi_app
response = self.make_response(self.handle_exception(e)) File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1403, in handle_exception
reraise(exc_type, exc_value, tb) File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1817, in wsgi_app
response = self.full_dispatch_request() File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1477, in full_dispatch_request
rv = self.handle_user_exception(e) File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1381, in handle_user_exception
reraise(exc_type, exc_value, tb) File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1475, in full_dispatch_request
rv = self.dispatch_request() File "/var/www/topicdm/venv/lib/python2.7/site-packages/flask/app.py", line
1461, in dispatch_request
return self.view_functionsrule.endpoint File "./app/views.py", line 479, in traffic
topic = Topic.query.filter_by(topic_domain=g.host).first() File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 2367, in first
ret = list(self[0:1]) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 2228, in getitem
return list(res) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 2438, in iter
return self._execute_and_instances(context) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py",
line 2453, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 729, in execute
return meth(self, multiparams, params) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py",
line 322, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params) File
"/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 826, in _execute_clauseelement
compiled_sql, distilled_params File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 893, in _execute_context
None, None) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 1159, in _handle_dbapi_exception
exc_info File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py",
line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb) File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 887, in _execute_context
conn = self._revalidate_connection() File "/var/www/topicdm/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py",
line 242, in _revalidate_connection
"Can't reconnect until invalid " sqlalchemy.exc.StatementError: Can't reconnect until invalid transaction is rolled back (original
cause: InvalidRequestError: Can't reconnect until invalid transaction
is rolled back) u'SELECT topics.id AS topics_id, topics.topic_name AS
topics_topic_name, topics.topic_keywords AS topics_topic_keywords,
topics.topic_desc AS topics_topic_desc, topics.topic_domain AS
topics_topic_domain, topics.topic_alias AS topics_topic_alias,
topics.topic_topdomain AS topics_topic_topdomain, topics.topic_theme
AS topics_topic_theme, topics.user_id AS topics_user_id,
topics.topic_update AS topics_topic_update \nFROM topics \nWHERE
topics.topic_domain = %s \n LIMIT %s' [immutabledict({})]

这不是 Flask 的问题呀,这是你的 sql 事务失败了但没回滚造成了数据库连接失败。

错误是sqlalchemy抛出。原因是你从pool拿的connection没有以session.commit或者session.rollback或者session.close的某一种放回pool里。这时connection的transaction没有完结(rollback or commit)。
而不知什么原因(recyle了,timeout了)你的connection又死掉了,你的sqlalchemy尝试重新连接。由于transaction还没完结,无法重连。
正确用法是确保每个web请求完了session.close或者session.rollback。把链接还回pool。

参见:
https://groups.google.com/forum/#!topic/sqlalchemy/qAMe78TV0M0

报错的时候 topic_domain 的值也是正常的....

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd18452.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年06月15日 - 09时41分23秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules