ndb异步保证在应用程序请求完成后执行?

编程入门 行业动态 更新时间:2024-10-17 15:28:10
本文介绍了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?

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

The original documentation (developers.google/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异步保证在应用程序请求完成后执行?

本文发布于:2023-11-14 09:46:03,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   完成后   ndb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!