ndb 模型的 _post_put_hook 什么时候有与 self 不同的未来? [英] When does an ndb Model's _post_put_hook have a future different from self?

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

问题描述

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 hooks 不检查 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.

所有 post-hook 在调用签名的末尾都有一个 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 的上下文中什么时候会不同?未来在这里的目的是什么?

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 hooks 不检查 RPC 是否成功;钩子无论失败如何运行.

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

未来可以包含执行 put() 时失败的原因.当放置失败时,您可以使用此知识通过使用钩子来处理.例如,如果您的 _post_put_hook 负责根据模型的属性递增关联的 SUM 聚合模型,则它可以在尝试递增之前首先检查 put 是否成功关联的 SUM 聚合模型.我不相信 selffuture.get_result().get() 的价值会有所不同,只要 RPC 没有失败.

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 什么时候有与 self 不同的未来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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