[英]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.transactional
上increment_counter
,我得到“只有祖先查询被允许内部事务”的错误。
How can I fix this and make it atomic? 如何解决这个问题并使之原子化?
you could try to use sharding https://developers.google.com/appengine/articles/sharding_counters 您可以尝试使用分片https://developers.google.com/appengine/articles/sharding_counters
full source available at https://github.com/GoogleCloudPlatform/appengine-sharded-counters-python 完整资源可从https://github.com/GoogleCloudPlatform/appengine-sharded-counters-python获得
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.