Cloud Datastore: ways to avoid race conditions

六月ゝ 毕业季﹏ 提交于 2019-12-02 09:56:47

Wrap your get and put into a transaction. This will ensure you cannot stomp over a different update.

You can read more about transactions with the NDB Client Library documentation.

In your code, you could for example just use the NDB transaction decorator:

@ndb.transactional(retries=1)
def view1(request, key):
    user = ndb.Key(urlsafe=key).get()
    user.x = 1
    user.put()
    ...

@ndb.transactional(retries=1)    
def view2(request, key):
    user = ndb.Key(urlsafe=key).get()
    user.y = 2
    user.put()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!