在 AppEngine Python 中设置 read_policy

Setting read_policy in AppEngine Python

this document 中提到默认 read_policy 设置为 ndb.EVENTUAL_CONSISTENCY

在我从应用程序的数据存储版本中批量删除实体项目后,我继续读取旧数据,所以我试图弄清楚如何将其更改为 STRONG_CONSISTENCY没有成功,包括:

我得到的错误是

BadArgumentError: read_policy argument invalid ('STRONG_CONSISTENCY')

如何更改此默认设置?更重要的是,我如何确保 NDB 将去数据存储加载结果而不是依赖旧的缓存值? (请注意,在批量删除后,数据存储浏览器告诉我实体已消失。)

您无法更改该默认值,它也是唯一可用的选项。从您引用的文档中(未提及其他选项):

Description

Set this to ndb.EVENTUAL_CONSISTENCY if, instead of waiting for the Datastore to finish applying changes to all returned results, you wish to get possibly-not-current results faster.

通过检查 google.appengine.ext.ndb.context.py 文件(其中没有 STRONG_CONSISTENCY 定义)确认了同样的情况:

# Constant for read_policy.
EVENTUAL_CONSISTENCY = datastore_rpc.Configuration.EVENTUAL_CONSISTENCY

EVENTUAL_CONSISTENCY 通过 google.appengine.ext.ndb.__init__.pyndb 中结束:

from context import *
__all__ += context.__all__

可能能够使用这样的 hack 来避免错误:

from google.appengine.datastore.datastore_rpc import Configuration

...fetch(options=ndb.ContextOptions(read_policy=Configuration.STRONG_CONSISTENCY))

但是我认为这只适用于读取通过查询获得的键的实体,而不适用于获得键本身的列表,它来自查询的索引使用,它始终是最终一致的——你删除的实体的根本原因仍然出现在结果中(一段时间,直到索引更新)。来自 Keys-only Global Query Followed by Lookup by Key:

But it should be noted that a keys-only global query can not exclude the possibility of an index not yet being consistent at the time of the query, which may result in an entity not being retrieved at all. The result of the query could potentially be generated based on filtering out old index values. In summary, a developer may use a keys-only global query followed by lookup by key only when an application requirement allows the index value not yet being consistent at the time of a query.

可能感兴趣: