ndb模型的_post_put_hook何时会与自己的未来有所不同? [英] When does an ndb Model's _post_put_hook have a future different from self?

查看:102
本文介绍了ndb模型的_post_put_hook何时会与自己的未来有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google App Engine的ndb提供了 _post_put_hook(self, future) ,记录如下:

Google App Engine's ndb provides a _post_put_hook(self, future), documented as follows:

在put()之后运行的钩子

Hook that runs after put()

为了更好地理解此钩子,我想知道self何时与future参数的结果不同.

To understand this hook better, I am wondering when self will differ from the result of the future argument.

模型挂钩文档提供:

如果将后钩与异步API一起使用,则通过调用check_result(),get_result()或让异步方法的未来(在tasklet内)触发钩子. Post钩子不检查RPC是否成功;它不会检查RPC是否成功.钩子无论发生故障都会运行.

If you use post-hooks with asynchronous APIs, the hooks are triggered by calling check_result(), get_result() or yielding (inside a tasklet) an async method's future. Post hooks do not check whether the RPC was successful; the hook runs regardless of failure.

所有后钩在呼叫签名的末尾都有一个Future参数.此Future对象保存操作的结果.您可以在此Future上调用get_result()来检索结果;您可以确定get_result()不会被阻塞,因为在调用该挂钩时Future已完成.

All post- hooks have a Future argument at the end of the call signature. This Future object holds the result of the action. You can call get_result() on this Future to retrieve the result; you can be sure that get_result() won't block, since the Future is complete by the time the hook is called.

但是,当我像这样异步调用put时:

However, when I call put asynchronously like this:

from google.appengine.ext import ndb

class MyModel(ndb.Model):
   xyz = ndb.StringProperty()

   def _post_put_hook(self, future):
      print "self.xyz: {}, future.xyz: {}".format(
             self.xyz, future.get_result().get().xyz))

m = MyModel()
f = m.put_async()
f.wait()

m.xyz = 'abc'
f = m.put_async()
f.wait()

输出:

self.xyz: None, future.xyz: None
self.xyz: abc, future.xyz: abc

在'put_async'的上下文中,我认为可以合理地期望self是修改之前的模型,而future是现在保存的模型.否则,不清楚futureput上下文中的用途.

In the context of a 'put_async', I think one might reasonably expect the self to be the model before modification, and the future to be the model as now saved. Otherwise it is not clear what purpose future would have in the put context.

selffutureput上下文中何时会有所不同? future的目的是什么?

When would self and future be different in the context of a put? What is the purpose of the future here?

推荐答案

我相信答案就在描述中:

I believe the answer is in the description:

Post钩子不检查RPC是否成功. 挂钩 不管失败而运行.

Post hooks do not check whether the RPC was successful; the hook runs regardless of failure.

将来执行put()时,可能包含失败的原因.您可以使用挂钩使用此知识来处理放置失败的情况.例如,如果您的_post_put_hook负责根据模型的属性来递增关联的SUM聚合模型,则它可以先尝试查看put是否成功,然后再尝试递增关联的SUM聚合模型.只要RPC没有失败,我相信selffuture.get_result().get()的值将不会有所不同.

The future can contain the reason for a failure when doing a put(). You can use this knowledge to handle when a put fails by using a hook. For instance, if your _post_put_hook was responsible for incrementing an associated SUM aggregation model based on properties of the model, it could first check to see if the put succeeded before trying to increment the associated SUM aggregation model. I do not believe the value of self and future.get_result().get() will ever be different, as long as the RPC did not fail.

总是有另一个请求在此请求的put之后但在_post_put_hook执行之前更新模型的情况,其中future.get_result().get(use_cache=False)可能返回不同的值.

There is always the possibility that another request updated the model after this request's put but before the _post_put_hook was executed, in which future.get_result().get(use_cache=False) may return a different value.

这篇关于ndb模型的_post_put_hook何时会与自己的未来有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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