AppEngine中的异步请求 [英] Asynchronous requests in AppEngine

查看:84
本文介绍了AppEngine中的异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基本上执行以下操作的应用程序:

I'm building an app that essentially does the following:

  • 让用户输入某些参数.
  • 将这些参数传递到后端,并根据这些参数启动任务 参数.
  • 任务完成后,将用户重定向到另一个显示页面 任务的结果.
  • Get the user to enter certain parameters.
  • Pass those params to the backend and start a task based on those params.
  • When the task is complete redirect the user to another page showing the results of the task.

这里的问题是该任务预计将花费很长时间.因此,我希望将请求异步化. appengine允许吗?

The problem here is that the task is expected to take quite long. I was thus hoping to make the request asynchronous. Does appengine allow this ?

如果没有,我有什么选择?我正在查看任务队列的文档.虽然它满足了我要执行的操作的一部分,但是我不清楚在任务完成时队列如何通知客户端,以便可以启动重定向.

If not, what are my options ? I was looking at the documentation for task queues. While it satisfies part of what I'm trying to do, I'm not very clear on how the queue notifies the client when the task is complete, so that the redirect can be initiated.

此外,如果必须将任务的结果返回给调用客户端本身怎么办?有可能吗?

Also, what if the results of the task have to be returned to the calling client itself ? Is that possible ?

推荐答案

您不能(不应真的)等待完成,GAE并非为此而设计.只需启动任务,获取任务ID(唯一,将其持久保存在应用程序中),然后将ID发送回客户端即可响应启动请求.

You can't (shouldn't really) wait for completion, GAE is not designed for that. Just launch the task, get a task ID (unique, persisted it in the app) and send the ID back to the client in the response to the launch request.

客户端可以通过轮询(以合理的速率)或仅按需检查状态页面(您可以使用ID查找正确的任务).您甚至可以在该页面上添加进度/ETA信息,如果需要的话.

The client can check, either by polling (at a reasonable rate) or simply on-demand, that status page (you can use the ID to find the right task). You can even add a progress/ETA info on that page, down the road if you so desire.

任务完成后,来自客户端的下一个状态检查请求可以重定向到您提到的结果页面.

After the task completes the next status check request from the client can be redirected to the results page as you mentioned.

此问题解答也可能会有所帮助,这是一个非常相似的场景,仅使用延迟的库:

This Q&A might help as well, it's a very similar scenario, only using the deferred library: How do I return data from a deferred task in Google App Engine

更新:

任务队列优于延迟库,因为延迟库可以使用

The Task Queues are preferable to the deferred library, the deferred functionality is available using the optional countdown or eta arguments to taskqueue.add():

  • 倒数-将来运行或租用此任务的时间(以秒为单位).默认为零.如果不指定此参数 您指定了eta.

  • countdown -- Time in seconds into the future that this task should run or be leased. Defaults to zero. Do not specify this argument if you specified an eta.

eta-一个datetime.datetime,用于指定任务应运行的绝对最早时间.您不能在以下情况下指定此参数 指定了倒数计时参数.这个说法可以是时间 时区感知或未时区,或设置为过去的时间.如果 参数设置为无,默认值为现在.对于拉任务,否 工人可以在eta指示的时间之前租用任务 论点.

eta -- A datetime.datetime that specifies the absolute earliest time at which the task should run. You cannot specify this argument if the countdown argument is specified. This argument can be time zone-aware or time zone-naive, or set to a time in the past. If the argument is set to None, the default value is now. For pull tasks, no worker can lease the task before the time indicated by the eta argument.

这篇关于AppEngine中的异步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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