Python 如何从线程到flask应用程序获取异步更新?

Python 如何从线程到flask应用程序获取异步更新?,python,flask,Python,Flask,我有一个烧瓶应用程序开始几个并行线程。每个线程开始一个耗时的测量。到目前为止,我可以启动所有线程,并等待它们完成后再更新页面 app.py: import time import threading import random from flask import render_template class AsynchronWorker(threading.Thread): def __init__(self, tc_name): threading.Thread._

我有一个烧瓶应用程序开始几个并行线程。每个线程开始一个耗时的测量。到目前为止,我可以启动所有线程,并等待它们完成后再更新页面

app.py:

import time
import threading
import random

from flask import render_template

class AsynchronWorker(threading.Thread):
    def __init__(self, tc_name):
        threading.Thread.__init__(self)
        self.tc_name = tc_name
        self.duration = 0

    def get_tc_name(self):
        return self.tc_name

    def get_duration(self):
        return self.duration

    def run(self):
        start_time = time.time_ns()
        time.sleep(random.randint(1, 5))
        self.duration = time.time_ns() - start_time


app = Flask(__name__)
test_cases = {"Upload": 0, "Download": 0}

@app.route("/")
def index():
    return render_template(template_name_or_list="index.html",
                           test_cases=test_cases)

@app.route("/start")
def start():
    threads = []
    results = {}
    for tc in test_cases:
        thread = AsynchronWorker(tc_name=tc)
        threads += [thread]
        thread.start()
    for x in threads:
        x.join()
        results[x.get_tc_name()] = x.get_duration()
    return render_template(template_name_or_list="index.html",
                           test_cases=results)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5005, debug=True)
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form>
        {% for name in test_cases.keys() %}
        <h2> {{ name }} run for {{ test_cases[name] }} ns.</h2>
        {% endfor %}
        <a href="/start"><input type="button" name="start" value="Start"></a>
    </form>
</body>
</html>

标题
{%用于测试用例中的名称。keys()%}
{{name}}为{{test_cases[name]}n运行。
{%endfor%}
我要寻找的是一个解决方案,每次线程完成时页面都会更新。到目前为止,我只发现一些使用ajax或socketIO的过时示例。但是没有一个为我工作