'Flask using thread to run code after/alognside return

I am trying to run code after I return my template in flask

Here is sample code

@app.route("/hello")
def Hello():
    thread = threading.Thread(target=long_running_task)
    thread.start()
    return 'hello'

def long_running_task():
    time.sleep(5)
    return redirect("/goodbye")

Is this possible? Why when this code is ran it does not work as intended? /hello should load, then after 5 seconds it should redirect, but it never redirects and I believe the thread is never ran.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source