How do I return data from a deferred task in Google App Engine

前端 未结 3 596
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 05:27

Original Question

I have a working version of my web application that I am trying to upgrade at the moment, and I\'m running into the issue of having a task which

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 05:55

    I'm not familiar with GAE, but this is a fairly generic question, so I can give you some advice.

    Your general idea is correct, so I'm just going to expand on it. The workflow could look like this:

    1. You get the request to create the lineups. You create a new entity in the datastore for it. It should contain an ID (you'll need it to retrieve the result later) and a status (PENDING|DONE|FAILED). You can also save the data from the request, if that's useful to you.
    2. You defer the computation and return a response right away. The response will contain the ID of the task. When the computation is done, it will save the result of the task in the Datastore and update the status of the task. That result will contain the task ID, so that we can easily find it.
    3. Once the frontend receives the ID, it starts polling for the result. Using setTimeout or setInterval you send requests with the task ID to the server (this is a separate endpoint). The server checks the status of the task, and returns the result if it's done (error if failed).
    4. The frontend gets the data and stops polling.

提交回复
热议问题