8

I am using ndb to write a profiling model that logs some data per application request. Each request calls a ndb request by ndb.put_async to log the data, while the client do not care about the result. In essence, I do not want the application request to wait for saving statistics data for profiling.

However, I was confused about the explanation from the official documentation. If an application request has finished before the ndb request finishes, would the ndb request still be guaranteed to finish? The documentation indicates that

if the request handler exists too early, the put might never happen

Under what criteria would this happen? Does this mean that regardless of whether a user care about the result, future.get_result needs to be called anyway just to make sure the ndb request is performed?

The original documentation (https://developers.google.com/appengine/docs/python/ndb/async) says:

In this example, it's a little silly to call future.get_result: the application never uses the result from NDB. That code is just in there to make sure that the request handler doesn't exit before the NDB put finishes; if the request handler exits too early, the put might never happen. As a convenience, you can decorate the request handler with @ndb.toplevel. This tells the handler not to exit until its asynchronous requests have finished. This in turn lets you send off the request and not worry about the result.

4

2 回答 2

7

如果应用程序请求在 ndb 请求完成之前完成,是否仍能保证 ndb 请求完成?

不。

这是否意味着无论用户是否关心结果,都需要调用future.get_result 以确保执行ndb 请求?

基本上是的,但您可以使用 ndb.toplevel 装饰器为方便起见,这样您就不必显式地等待结果。也就是说,我认为这不是你想要的。

可能任务队列是你想要的。请检查一下。

于 2012-07-24T02:19:40.180 回答
1

感谢您的澄清。一般的 RPC(非 NDB)怎么样 - 例如,memcache.Client() 中的 incr_async()?撇开这是一个非常非常快的 RPC 调用不谈,是否保证 RPC 会完成?

即,下列哪项是正确的:

(a) 基础设施中有一些东西会在完成请求之前等待所有已知的 RPC

(b) 请求将完成,并且异步 RPC 也将完成,无论请求何时完成

(c) 机上 RPC 正式取消

(d) 别的东西?

于 2012-07-24T19:50:16.383 回答