我正在使用 Python 和 ndb 做一些工作,但不明白为什么。我将发布案例和上面的代码:
模型.py
class Reference(ndb.Model):
kind = ndb.StringProperty(required=True)
created_at = ndb.DateTimeProperty(auto_now_add=True)
some_id = ndb.StringProperty(indexed=True)
data = ndb.JsonProperty(default={})
这些测试在交互式控制台和 dev_appserver.py 的 --high_replication 选项中运行:
测试 1
from models import Reference
from google.appengine.ext import ndb
import random
some_id = str(random.randint(1, 100000000000000))
key_id = str(random.randint(1, 100000000000000))
Reference(id=key_id, some_id=some_id, kind='user').put()
print Reference.query(Reference.some_id == some_id, Reference.kind == 'user').get()
# output:
# >> None
为什么 ?????现在,让我们在打印之前添加一个 sleep(1):
测试 2
from models import Reference
from google.appengine.ext import ndb
import random
from time import sleep
some_id = str(random.randint(1, 100000000000000))
key_id = str(random.randint(1, 100000000000000))
Reference(id=key_id, some_id=some_id, kind='user').put()
sleep(1)
print Reference.query(Reference.some_id == some_id, Reference.kind == 'user').get()
# output:
# >> Reference(key=Key('Reference', '99579233467078'), createdAt=datetime.datetime(2013, 1, 31, 16, 24, 46, 383100), data={}, kind=u'user', some_id=u'25000975872388')
K,假设它正在模拟将文档传播到所有 Google 表格的时间,我永远不会在我的代码中休眠。现在,让我们移除睡眠并添加一个父母!
测试 3
from models import Reference
from google.appengine.ext import ndb
import random
from time import sleep
some_id = str(random.randint(1, 100000000000000))
key_id = str(random.randint(1, 100000000000000))
Reference(id='father', kind='father').put()
Reference(parent=ndb.Key(Reference, 'father'), id=key_id, some_id_id=some_id, kind='user').put()
print Reference.query(Reference.some_id == some_id, Reference.kind == 'user', ancestor=ndb.Key(Reference, 'father')).get()
# output:
# >> Reference(key=Key('Reference', '46174672092602'), createdAt=datetime.datetime(2013, 1, 31, 16, 24, 46, 383100), data={}, kind=u'user', some_id=u'55143106000841')
现在这很混乱!只需设置一个父母并给我强大的一致性!为什么 ?如果需要提供强一致性,为什么在默认情况下将所有文档插入数据存储区时不让所有文档具有相同的父级?也许我做错了,有办法做得更好。请有人指导我!
提前致谢