python - flask_sqlalchemy 循环查询结果一样
PHPz
PHPz 2017-04-18 09:15:53
0
2
631

使用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)
  • 数据表中记录的 status 全部为 0,运行以上代码 持续输出 None ,不中断进程,手动修改数据表中一条记录status=1 以上代码还是照常输出 None 相当于task还是未获取到值,求解?

PHPz
PHPz

学习是最好的投资!

reply all(2)
大家讲道理

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

小葫芦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!