1
我有一个功能,抓取网络的数据和计算搜索的分数。但是,这可能需要一段时间,有时在完成执行之前网页会超时。线程完成后,如何更改Flask中呈现的模板?
所以我创建了一个独立的线程来执行函数,loading.html
告诉客户端数据仍在被收集。一旦该功能在线程中结束,如何重新加载网页以显示output.html
,以显示分数。
这是我至今一个简单的版本:
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()
我怎么路线我的应用程序render_template('output.html', x=score)
一次something()
线程里面th
结束?
我想避免像redis这样的任务队列,因为我想部署这个应用程序在网络上,我不想招致费用(这是一个实验和业余爱好)。
与代码了详细的解答将有很大的帮助,因为我是新来的烧瓶中,多线程
这工作就像我想要的一样!谢谢!不知道为什么我没有考虑过编写javascript函数 – Apara