2

我正在使用 Django 的 ORM 从数据库中获取新添加的条目并将它们传递给消息队列。我在无限循环中执行此操作,每次循环迭代中的问题即使在此脚本运行时添加/删除/编辑条目,我也会得到相同的查询集,

代码如下:

while True :
    sleep(10 seconds)
    # Below is the problem line, I get the same query-set every time in new_objects
    # even when I have added/deleted/edited entries while this daemon is running.
    new_objects = Model.objects.filter(some condition)

    # Process new_objects and send them to MQ
    .
    . and so on

我应该怎么做才能反映每次迭代中的最新数据?

4

1 回答 1

4

这与缓存无关。这与交易有关。

默认情况下,连续运行的脚本在单个事务中运行,因此永远不会看到该事务之外的更改。您需要在每次迭代时手动启动新事务 - 请参阅事务文档以了解如何执行此操作。

于 2011-07-31T08:39:24.423 回答