ndb异步保证在应用程序请求完成后执行? [英] Is ndb async guaranteed to execute after application request has finished?

查看:88
本文介绍了ndb异步保证在应用程序请求完成后执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ndb编写一个分析模型,以记录每个应用程序请求的一些数据。每个请求都会通过ndb.put_async调用ndb请求来记录数据,而客户端不关心结果。实质上,我不希望应用程序请求等待保存用于分析的统计数据。

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.

然而,我对官方文档的解释感到困惑。如果一个应用程序请求在ndb请求结束之前完成,ndb请求是否仍能保证完成?该文档表明,如果请求处理程序存在得太早,则可能永远不会发生

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


这会发生什么样的标准?这是否意味着无论用户是否关心结果,future.get_result都需要被调用,以确保ndb请求被执行?

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?

原始文档( https://developers.google.com/appengine/docs/python/ndb/ async )表示:

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


在这个例子中,调用future.get_result有点愚蠢:
应用程序从不使用NDB的结果。该代码仅存在于
中,以确保在NDB将
完成之前请求处理程序不会退出;如果请求处理程序太早退出,put可能永远不会发生
。为了方便起见,您可以使用
@ ndb.toplevel来装饰请求处理程序。这告诉处理程序在其
异步请求完成之前不要退出。这反过来可以让你发送
的请求,而不用担心结果。

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.


推荐答案


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

If an application request has finished before the ndb request finishes, would the ndb request still be guaranteed to finish?

没有。

No.


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

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?

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

Basically yes, but you can use ndb.toplevel decorator for the convenience so that you don't have to wait for the result explicitly. That said, I don't think this is what you want.

可能 taskqueue 是你想要的。请检查出来。

Probably taskqueue is what you want. Please check it out.

这篇关于ndb异步保证在应用程序请求完成后执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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