我试图实现强一致性。让我们把我的模型PVPPlayer
:AppEngine实现强一致性
class PVPPlayer(ndb.Model):
points = ndb.IntegerProperty()
为模型中的每个关键是这样创建的:
pvp_player = PVPPlayer(key=ndb.Key(Profile, "test_id", PVPPlayer, "test_id"))
其中Profile
是父模型:
class Profile(ndb.Model):
def build_key(cls, some_id):
return ndb.Key(cls, some_id)
我有2个REST API URL:
1) update_points
2) get_points
在1)我做的:
# I use transaction because I have to update all the models in single batch
@ndb.transactional(xg=True, retries=3)
def some_func(points):
pvp_player = ndb.Key(Profile, "test_id", PVPPlayer, "test_id").get()
pvp_player.points += points
pvp_player.put()
# update other models here`
在2)我做的:
pvp_player = ndb.Key(Profile, "test_id", PVPPlayer, "test_id").get()
return pvp_player.points`
我的流程是这样的:
1) update_points()
2) get_points()
3) update_points()
4) get_points()`
...
问题:
使用get()
保证强一致性o我不明白的是为什么有时候由于get_points()
的结果,我得到的数据像陈旧的数据根本没有更新。
例:
POST get_points -> 0
POST sleep 1-3 sec
POST update_points -> 15
POST sleep 1-3 sec
POST get_points -> 15
POST sleep 1-3 sec
POST update_points -> 20
POST sleep 1-3 sec
POST get_points -> 15 !!!`
是您的#2功能,还装饰有'@ndb .transactional'?另外,我认为#1中的'update_points'函数名称确实是'update_points',对吗? –
>你的#2功能是否也用@ ndb.transactional装饰过? 我试图为#2添加@ ndb.transactional,但它并不重要 - 它仍然不时给我陈旧的数据 >#1中的update_points函数名称确实是update_points,对不对? 绝对 –
对不起,我的意思是'some_func'是'update_points'? :) –