AppEngine 中的异步请求
Asynchronous requests in AppEngine
我正在构建一个主要执行以下操作的应用程序:
- 让用户输入某些参数。
- 将这些参数传递给后端并根据这些参数启动任务
参数。
- 任务完成后将用户重定向到显示的另一个页面
任务结果。
这里的问题是任务预计需要很长时间。因此,我希望使请求异步。 Appengine 是否允许这样做?
如果没有,我有什么选择?我正在查看任务队列的文档。虽然它满足了我正在尝试做的部分工作,但我不太清楚队列在任务完成时如何通知客户端,以便可以启动重定向。
此外,如果任务的结果必须返回给调用客户端本身怎么办?这可能吗?
您不能(真的不应该)等待完成,GAE 不是为此设计的。只需启动任务,获取任务 ID(唯一,将其保存在应用程序中)并在对启动请求的响应中将该 ID 发送回客户端。
客户端可以通过轮询(以合理的速率)或简单地 on-demand 检查该状态页面(您可以使用 ID 找到正确的任务)。如果您愿意,您甚至可以在该页面上添加 progress/ETA 信息。
任务完成后,来自客户端的下一个状态检查请求可以重定向到您提到的结果页面。
这个问答可能也有帮助,这是一个非常相似的场景,只使用了延迟库:
更新:
Task Queues are preferable to the deferred library, the deferred functionality is available using the optional countdown
or eta
arguments to taskqueue.add():
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 -- 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 是否允许这样做?
如果没有,我有什么选择?我正在查看任务队列的文档。虽然它满足了我正在尝试做的部分工作,但我不太清楚队列在任务完成时如何通知客户端,以便可以启动重定向。
此外,如果任务的结果必须返回给调用客户端本身怎么办?这可能吗?
您不能(真的不应该)等待完成,GAE 不是为此设计的。只需启动任务,获取任务 ID(唯一,将其保存在应用程序中)并在对启动请求的响应中将该 ID 发送回客户端。
客户端可以通过轮询(以合理的速率)或简单地 on-demand 检查该状态页面(您可以使用 ID 找到正确的任务)。如果您愿意,您甚至可以在该页面上添加 progress/ETA 信息。
任务完成后,来自客户端的下一个状态检查请求可以重定向到您提到的结果页面。
这个问答可能也有帮助,这是一个非常相似的场景,只使用了延迟库:
更新:
Task Queues are preferable to the deferred library, the deferred functionality is available using the optional countdown
or eta
arguments to taskqueue.add():
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 -- 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.