GAE Python NDB .put在开发中不同步(但在生产环境中工作)? [英] GAE Python NDB .put not synchronous on development (but works in production)?

查看:101
本文介绍了GAE Python NDB .put在开发中不同步(但在生产环境中工作)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容应创建一个 Counter 模型并使用(延迟)任务将计数器增加到10。 c $ c>应该使用 count = 10 创建一个 Counter 对象。这发生在生产中。在开发(localhost)中创建了多个 Counter 对象,其中最大的对象是10:


我怀疑这是因为 put 在开发中不同步(但似乎总是在生产中)。有没有办法使它们同步?



以下代码片段:

  class Counter(ndb.Model):
count = ndb.IntegerProperty(indexed = False)

def reset():
ndb.delete_multi(Counter ().fetch(keys_only = True,use_cache = False,use_memcache = False))
$ b def increment():
counter = Counter()。query()。get(use_cache = False ,use_memcache = False)
if counter:
counter = Counter(count = 0)
$ b $ counter.count + = 1
counter.put()

counter.count< 10:
deferred.defer(增量)

@ app.route('/')
def hello():
返回友好的HTTP问候语。
reset()
deferred.defer(increment)
return'Hello World!'

我有一个git repo,它重现了这种行为 here 。您可以在这里找到提交上次更改的提交 解决方案

制作'同步'很明显,但不能保证(在您的方法中)。在查询中找不到新创建的计数器,因此您的代码可能会创建多个计数器。



更多详情请见平衡Google云数据存储的强大和最终一致性文章。


The following below should create a Counter model and use (deferred) tasks to increment the counter to 10. Visiting '/' ought to create a single Counter object with count = 10. This happens in production. In development (localhost) multiple Counter objects are created with the largest being 10:

I suspect this is because the put is not synchronous on development (but appears to always be on production). Is there a way to make them synchronous?

Code snippet below:

class Counter(ndb.Model):
  count = ndb.IntegerProperty(indexed=False)

def reset():
  ndb.delete_multi(Counter().query().fetch(keys_only=True, use_cache=False, use_memcache=False))

def increment():
  counter = Counter().query().get(use_cache=False, use_memcache=False)
  if not counter:
    counter = Counter(count=0)

  counter.count += 1
  counter.put()

  if counter.count < 10:
    deferred.defer(increment)

@app.route('/')
def hello():
  """Return a friendly HTTP greeting."""
  reset()
  deferred.defer(increment)
  return 'Hello World!'

I have a git repo that reproduces this behavior here. You can find the commit that makes the last change here.

解决方案

The production 'synchronicity' is just apparent, it's not guaranteed (in your approach). It can always happen that a newly created counter is not found in the query, thus your code could create multiple counters.

More details in this Balancing Strong and Eventual Consistency with Google Cloud Datastore article.

这篇关于GAE Python NDB .put在开发中不同步(但在生产环境中工作)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆