当函数处于无限循环中时,wep2py不会写入sql库

时间:2014-12-02 18:02:50

标签: python sql web2py

我有一个写入sql db的函数,如果我运行它只有一次我工作正常,但如果它在while 1:,它不会写任何东西到db

代码示例:

def to_db_f(f):
    for b in f:
        db.a.update_or_insert((db.a.event_id == b['event_id']), db.a._filter_fields(b))

def main():
    while 1:
        result = some_function()
        to_db_f(result)

1 个答案:

答案 0 :(得分:0)

在无限循环中,数据不会在数据库中提交。 Web2py自动从控制器提交函数末尾的数据。 但在你的情况下,函数main()永远不会退出,因此Web2py不会提交数据。

如果您想在循环播放期间查看更新的数据,请在db.commit()之后立即致电db.a.update_or_insert

def to_db_f(f):
    for b in f:
        db.a.update_or_insert((db.a.event_id == b['event_id']), db.a._filter_fields(b))
        db.commit()

def main():
    while 1:
        result = some_function()
        to_db_f(result)

Book中解释了这一点。