【发布时间】:2016-12-25 06:44:23
【问题描述】:
我有一个函数可以在网络上抓取数据并计算搜索分数。但是,这可能需要一段时间,有时网页在完成执行之前会超时。
所以我创建了一个单独的线程来执行该函数和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()
一旦线程something() 内的th 完成,我如何将我的应用程序路由到render_template('output.html', x=score)?
我想避免像 redis 这样的任务队列,因为我想在网络上部署这个应用程序并且我不想产生费用(这更多是一个实验和爱好)。
因为我是烧瓶和多线程的新手,所以用代码详细回答会有很大帮助
【问题讨论】:
标签: python multithreading heroku flask backgroundworker