Asynchronous requests in AppEngine

心已入冬 提交于 2020-01-16 03:25:07

问题


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.

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 ?


回答1:


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.

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

Update:

The 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.



来源:https://stackoverflow.com/questions/35030799/asynchronous-requests-in-appengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!