When does the ndb _post_put_hook model have a future different from itself?

The Google App Engine ndb provides _post_put_hook(self, future) , documented as follows:

The hook that starts after put ()

To understand this hook better, I wonder when self will be different from the result of the future argument.

Model Hooks documentation provides:

If you use post-hooks with asynchronous APIs, hooks are triggered by calling check_result (), get_result () or receiving (inside the tasklet) an asynchronous use method in the future. Post-hooks do not check if the RPC was successful; The hook works regardless of failure.

All post-hooks have a Future argument at the end of the call signature. This Future object contains the result of the action. You can call get_result () in this Future to get the result; you can be sure that get_result () will not be blocked, since the future will be completed by the time the hook is called.

However, when I call put asynchronously as follows:

 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() 

Outputs:

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

In the context of "put_async", I think one would expect that self would be a model before modification, and future would become a model that is now saved. Otherwise, it is not clear what the future goal would be in the context of put .

When will self and future differ in the context of put ? What is the future goal here?

+6
source share
1 answer

I believe the answer is in the description:

Post-hooks do not check if the RPC was successful; the hook is executed regardless of failure .

The future may contain the reason for the failure of put() . You can use this knowledge to handle when a failure fails using a hook. For example, if your _post_put_hook was responsible for increasing the associated SUM aggregation model based on the model properties, he might first check if put successful before trying to increase the corresponding SUM aggregation model. I do not believe that the value of self and future.get_result().get() will ever be different if RPC does not work.

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

+10
source

All Articles