1 回答

TA貢獻1851條經驗 獲得超3個贊
好吧,雖然我還沒有讀到為什么它表現成這樣,但至少現在我終于找到了解決問題需要做些什么:
一旦每次迭代中的查詢都實現了其目的(在我的情況下,從需要處理的數據庫中獲取第一條記錄,然后對其進行處理),我接下來需要調用以下內容:并解決了我的問題。session.expire_all()
因此,在我上面的示例中,它現在應該如下所示:
def main():
while True:
item = Session.query(Queue).order_by(Queue.id.asc()).filter_by(status=0).first()
if item:
try:
item.status = 1
Session.commit()
print(f'Item id: {item.id}')
Session.expire_all() # <---- Add this line
except:
print('error')
time.sleep(2)
這是SQLAlchemy文檔中大致指向這個方向的部分:SQLAlchemy - Refreshing / Expireing
添加回答
舉報