简体   繁体   English

GAE数据存储区中的运行条件

[英]Run condition in GAE datastore

class ModelCount(db.Model):
    type = db.StringProperty(required=True,default='Default-type') 
    count = db.IntegerProperty(required=True, default=0) #Current counter

    def to_dict(self):
       d = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
       d["id"] = self.key().id()
       return d

    #Increments counter
    @staticmethod
    def increment_counter(en_name):
         modelCount = ModelCount.all().filter('type',en_name).get()
         if modelCount:
            modelCount.count += 1
            modelCount.put()
         else:
            modelCount = ModelCount(type=en_name, count=1)
            modelCount.put()

In the above code ( increment_counter ), I am reading the count from ModelCount and incrementing it by one. 在上面的代码( increment_counter )中,我正在从ModelCount读取计数并将其递增1。 I face run condition in increment_counter method when the server receives multiple requests. 当服务器接收到多个请求时,我会在increment_counter方法中遇到运行条件。 So I want to make increment_counter atomic. 所以我想让increment_counter计数器为原子。 If I use @db.transactional on increment_counter , I get "Only ancestor queries are allowed inside transactions" error. 如果我使用@db.transactionalincrement_counter ,我得到“只有祖先查询被允许内部事务”的错误。

How can I fix this and make it atomic? 如何解决这个问题并使之原子化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM