我正在使用 Redis 队列处理 Flask 应用程序上的后台任务。当我按下 HTML 模板上的按钮时,update()
函数使 task()
函数在后台工作。单击按钮后,我传递变量 message
,它在 HTML 上显示一条消息,让用户知道任务正在完成。但是当任务完成后,我想让用户知道。如何重定向到另一个页面,或至少更改 message
内容?
我看过这个答案:How do I return flask render_template after the Redis background job is done? 但我不希望页面每隔一定时间更新一次。我想在后台任务完成后进行一次更新。
示例代码:
app = Flask(__name__)
q = Queue(connection=conn)
def task():
print("Starting background task")
time.sleep(10)
return "Background task finished"
# Redirect no another page
@ app.route("/update")
def update():
job = q.enqueue(task)
message = "Doing the task..."
return redirect(url_for("index.html", message=message))