我有一个函数可以在网络上抓取数据并计算搜索分数。但是,这可能需要一段时间,有时网页会在完成执行之前超时。
因此,我创建了一个单独的线程来执行该函数和 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()
如何将我的应用程序路由到线程th
内的render_template('output.html', x=score)
一次something()
> 结束了吗?
我试图避免像 Redis 这样的任务队列,因为我想在网络上部署这个应用程序,并且我不想产生费用(这更多的是一种实验和爱好)。
带有代码的详细答案将会有很大帮助,因为我是 flask 和多线程的新手
最佳答案
一种简单的方法是向 thread_status 端点发出循环 Ajax 请求,该端点为您提供有关当前正在运行的任务的信息。
import time
from flask import Flask, jsonify
from flask import render_template
from threading import Thread
app = Flask(__name__)
th = Thread()
finished = False
@app.route("/")
def init():
return render_template('index.html')
@app.route("/", methods=['POST'])
def load():
global th
global finished
finished = False
th = Thread(target=something, args=())
th.start()
return render_template('loading.html')
def something():
""" The worker function """
global finished
time.sleep(5)
finished = True
@app.route('/result')
def result():
""" Just give back the result of your heavy work """
return 'Done'
@app.route('/status')
def thread_status():
""" Return the status of the worker thread """
return jsonify(dict(status=('finished' if finished else 'running')))
if __name__ == "__main__":
app.run(debug=True)
因此,在您的 loading.html 中,只需插入一个循环 Ajax get()
请求:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
var refresh_id = setInterval(function() {
$.get(
"{{ url_for('thread_status') }}",
function(data) {
console.log(data);
if (data.status == 'finished') {
window.location.replace("{{ url_for('result') }}");
}
}
)}
, 1000);
});
</script>
</head>
<body>
<p>Loading...</p>
</body>
</html>
如果您愿意,您甚至可以通过进度计数器附加此内容。但您需要注意防止线程多次运行。
关于python - 当线程完成时,如何更改 Flask 中渲染的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319199/