I have a function that crawls the web for data and computes a score for the search. However, this can take a while and sometimes the webpage times out before finishing execution.
So I created a separate thread that executes the function and loading.html
that tells the client that data is still being collected. Once the function ends in the thread, how do I reload the webpage to display output.html
that displays the score.
This is a simpler version of what I have so far:
from flask import Flask
from flask import render_template
from threading import Thread
app = Flask(__name__)
@app.route("/")
def init():
return render_template('index.html')
@app.route("/", methods=['POST'])
def load():
th = Thread(target=something, args=())
th.start()
return render_template('loading.html')
def something():
#do some calculation and return the needed value
if __name__ == "__main__":
app.run()
How do I route my app to render_template('output.html', x=score)
once something()
inside the thread th
finishes?
I am trying to avoid task queues like redis since I want to deploy this app on the web and I don't want to incur charges (this is more of an experiment and hobby).
A detailed answer with code would help a lot since I am new to flask and multithreading