Google App Engine DB在返回之前未执行完成

时间:2016-06-19 17:32:05

标签: python sql database google-app-engine

我目前正在使用Google应用引擎的内置数据库。似乎当我运行put()将元组插入数据库时​​,即使元组尚未完全插入,函数也会返回。这是代码:

new_user = Users(username=username_input, hashed_password=get_hashed_password(username_input, password))
new_user.put()

existing_user = None
while not existing_user:    
        print "existing_user still not in DB"

        #tries to get the user that was put into the DB
        existing_user = db.GqlQuery("SELECT * FROM Users WHERE username=:username_input", username_input=username_input).get()
print "existing_user in DB"

当我运行此代码时,我得到以下输出,

existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user still not in DB
existing_user in DB

为什么会这样?返回之前,put()不应该将元组放入数据库中吗?

1 个答案:

答案 0 :(得分:1)

数据存储区“最终一致”。这意味着在更改(插入/更新/删除)之后,查询可以(将)在短时间内返回旧数据视图。使用SDK,这将延迟1秒进行模拟。在实时环境中,通常小于此值,但偶尔会更多。

要获得数据的一致视图,您需要直接使用其键获取实体,或者将实体分组在父项下(将它们放在实体组中),然后使用具有祖先约束的查询。

您可以阅读更多herehere