How to redirect on Flask after Redis background task is done?

Viewed 561

I'm using Redis Queue to process a background task on a Flask app. When I press a button on the HTMLtemplate, the update() function make the task() function work in the background. After clicking button, I pass the variable message, which shows a message on the HTML, letting know the user that the task is being done. But when the task is done,I want to let the user know it. How can I redirect to another page, or change the message content at least?

I've seen this answer: How do I return flask render_template after the Redis background job is done? But I do not want the page to update every certain amount of time. I want a single update when background task is done.

Example code:

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))

0 Answers
Related