V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
duyuyouci
V2EX  ›  Python

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

  •  
  •   duyuyouci · 2019-06-05 11:47:19 +08:00 · 2672 次点击
    这是一个创建于 1786 天前的主题,其中的信息可能已经有所发展或是发生改变。
    时不时会出现 This result object does not return rows. It has been closed automatically.报错,大佬们,请问是什么原因,面向百度和谷歌了半天,也没找到合适的解决办法,求助
    8 条回复    2019-06-05 17:45:02 +08:00
    duyuyouci
        2
    duyuyouci  
    OP
       2019-06-05 14:19:18 +08:00
    @liuguichao 感谢🙏,我研究研究
    rogwan
        3
    rogwan  
       2019-06-05 14:42:31 +08:00 via Android
    @duyuyouci 是在 celery 中直接建立的 sql 连接?还是别的框架的上下文连接?
    robinlovemaggie
        4
    robinlovemaggie  
       2019-06-05 15:09:40 +08:00
    字面意思:因为(查询)结果对象没有返回数据行,所以被自动关闭。这不像是错误,倒是像是 warning。
    duyuyouci
        5
    duyuyouci  
    OP
       2019-06-05 15:32:55 +08:00
    @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
        6
    duyuyouci  
    OP
       2019-06-05 15:33:41 +08:00
    @robinlovemaggie 确实不会引起系统错误,但是会影响用户使用😂
    rogwan
        7
    rogwan  
       2019-06-05 16:41:31 +08:00 via Android
    @duyuyouci 你说的这个问题我没有遇到完全一样的,但是 celery 阻塞假死,导致数据连接出错的情况到是遇到几次。后来处理耗时短的业务就直接 Thread 了,耗时长的加回滚,和异常重连,基本就解决啦。
    liuguichao
        8
    liuguichao  
       2019-06-05 17:45:02 +08:00
    还有一种可能,遍历 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().
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5692 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.