2016-06-19 20 views
0

我目前正在使用Google应用引擎的内置分贝。看起来,当我运行put()向数据库中插入一个元组时,即使该元组还没有被完全插入,该函数也会返回。下面的代码:Google App Engine数据库在返回之前不会执行完毕

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

数据存储“最终一致”。这意味着在更改(插入/更新/删除)之后,查询可以(将)在短时间内返回旧数据视图。使用SDK时,延迟时间为1秒。在现场环境下,它通常是,比这个少,但偶尔会更多。要获得数据的一致视图,您需要直接使用它们的键获取实体,或者将实体归入父级(将它们放在实体组中),然后使用具有祖先约束的查询。

您可以阅读更多herehere

相关问题