Home  >  Q&A  >  body text

python - flask_sqlalchemy 循环查询结果一样

使用flask_sqlalchemy 每隔5秒循环查询数据时,获取到的数据相同。

代码如下:

while True:
        task = TaskQueue.query.filter_by(status=0).order_by(TaskQueue.create_time.asc()).first()
        print(task)

        if task:
            pass
        else:
            time.sleep(5)
PHPzPHPz2765 days ago669

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 09:17:53

    I know how to solve it, but I don’t know the reason. Just add db.session.remove() when Task = None~~

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:17:53

    I have never used flask_sqlalchemy. According to your idea, sql should be like this. Find the first status=0 in ascending order by creation time
    select * from taskqueue where status=0 order by create_time asc limit 0, 1

    Theoretically, if the status in your data table is all 0, there should be data every time. The problem may be that your ORM is not connected to the specified library, or the first function does not work.

    It is recommended that you check if you can find the data first

    tasks = TaskQueue.query.filter_by(status=0)
    print len(tasks)

    Determine which link is the problem

    reply
    0
  • Cancelreply