2

问题已更新以包括取得的进展

我有以下代码,我的 celery 任务开始正常,我只是不知道应该将异步结果存储在哪里,以便稍后再查看

#!/usr/bin/env python

"""Page views."""

from flask import render_template, request
from flask import Flask

from celerytest import add

from time import sleep


app = Flask(__name__)

async_res = []

@app.route('/', methods=['GET', 'POST'])
def run():
    if request.method == 'GET':
        return render_template("template.html")
    else:
        form = request.form
        n1 = str(form.get("n1"))
        n2 = str(form.get("n2"))
        aysnc_res = add.delay(n1,n2)
        return render_template("loading.html")

@app.route('/loading')
def check_if_complete():
    if async_res.ready() == True:
        return render_template("template2.html", val=async_res.get())
    else:
        sleep(5)
        return render_template("loading.html")


if __name__ == '__main__':
    app.run()

似乎async_res在我的烧瓶应用程序中存储为全局变量会导致服务器错误。那么存储结果的最佳方法是什么,以便我可以在我的“加载页面”中检查它

4

1 回答 1

3

我最终能够将我的任务 ID 保存在 Flask 的会话字典中

请参见下面的代码:

#!/usr/bin/env python

"""Page views."""

from flask import render_template, request
from flask import Flask

from celerytest import add

from time import sleep


app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def run():
    if request.method == 'GET':
        return render_template("template.html")
    else:
        form = request.form
        n1 = str(form.get("n1"))
        n2 = str(form.get("n2"))
        aysnc_res = add.delay(n1,n2)
        session['TASK_ID'] = async_res.id
        return render_template("loading.html")

@app.route('/loading')
def check_if_complete():
    aysnc_res = session['TASK_ID']
    if async_res.ready() == True:
        return render_template("template2.html", val=async_res.get())
    else:
        sleep(5)
        return render_template("loading.html")


if __name__ == '__main__':
    app.run()
于 2013-08-09T13:55:51.333 回答