2014-09-02 120 views
0
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() 

运行条件在上面的代码(increment_counter),我从ModelCount读取计数和由一个递增它。当服务器收到多个请求时,我面对运行条件increment_counter方法。所以我想使increment_counter原子。如果我在increment_counter上使用@db.transactional,我会得到“只允许在事务内部进行祖先查询”错误。在GAE数据存储

我该如何解决这个问题并使之成为原子?

+0

你应该避免* *原子; GAE给你*最终一致性*,而不是原子数据存储。 – 2014-09-02 12:13:19

+0

但在我的情况下,我想保持一致性。我怎样才能实现它?我需要使用锁吗? – 2014-09-02 12:18:14

+0

你不能,不可靠。在任何情况下,你都会杀死任何和所有的性能,因为你必须等待你的数据被复制到世界各地广阔的Google数据中心网络中。 – 2014-09-02 12:18:43

回答