使用 sqlalchemy 和 celery 异步处理数据库查询时,时不时会报错

2019 年 6 月 5 日
 duyuyouci
时不时会出现 This result object does not return rows. It has been closed automatically.报错,大佬们,请问是什么原因,面向百度和谷歌了半天,也没找到合适的解决办法,求助
3559 次点击
所在节点    Python
8 条回复
liuguichao
2019 年 6 月 5 日
duyuyouci
2019 年 6 月 5 日
@liuguichao 感谢🙏,我研究研究
rogwan
2019 年 6 月 5 日
@duyuyouci 是在 celery 中直接建立的 sql 连接?还是别的框架的上下文连接?
robinlovemaggie
2019 年 6 月 5 日
字面意思:因为(查询)结果对象没有返回数据行,所以被自动关闭。这不像是错误,倒是像是 warning。
duyuyouci
2019 年 6 月 5 日
@rogwan 是在 celery 中实例化的 session()连接
@Print.task(bind=True,name="pf_order_print",max_retries=10, base=ErrorMsgTask)
def print_order(self, order_id, user_id=0):
from handlers.base.pub_func import OrderPrintBaseFunc
time_begin = datetime.datetime.now()
session = DBSession()
.......
像这样
duyuyouci
2019 年 6 月 5 日
@robinlovemaggie 确实不会引起系统错误,但是会影响用户使用😂
rogwan
2019 年 6 月 5 日
@duyuyouci 你说的这个问题我没有遇到完全一样的,但是 celery 阻塞假死,导致数据连接出错的情况到是遇到几次。后来处理耗时短的业务就直接 Thread 了,耗时长的加回滚,和异常重连,基本就解决啦。
liuguichao
2019 年 6 月 5 日
还有一种可能,遍历 ResultProxy 记录之前,需要先判断一下是否有返回行,直接 for 遍历会出错。
https://docs.sqlalchemy.org/en/13/core/connections.html?highlight=resultproxy#sqlalchemy.engine.ResultProxy.returns_rows

returns_rows
True if this ResultProxy returns rows.
I.e. if it is legal to call the methods fetchone(), fetchmany() fetchall().

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://study.congcong.us/t/571070

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX