线程完成后,如何在Flask中更改渲染的模板? [英] How do I change the rendered template in Flask when a thread completes?

查看:131
本文介绍了线程完成后,如何在Flask中更改渲染的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以在网上抓取数据并计算搜索得分.但是,这可能需要一段时间,有时网页会在完成执行之前超时.

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.

因此,我创建了一个单独的线程来执行该函数,并创建 loading.html 来告诉客户端仍在收集数据.函数在线程中结束后,如何重新加载网页以显示显示分数的 output.html .

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()

如何将我的应用程序路由到第 th something()一旦,将我的应用路由到 render_template('output.html',x = score)>完成?

How do I route my app to render_template('output.html', x=score) once something() inside the thread th finishes?

我想避免像redis这样的任务队列,因为我想在Web上部署此应用程序,并且我不想产生任何费用(这更多是实验和业余爱好).

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).

代码的详细答案对我很有帮助,因为我是flask和multithreading的新手.

A detailed answer with code would help a lot since I am new to flask and multithreading

推荐答案

一种简单的方法是向 thread_status 端点发出循环Ajax请求,从而向您提供有关当前正在运行的任务的信息.

An easy way is making cyclic Ajax requests to a thread_status endpoint that gives you information about the currently running task.

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()请求:

So in your loading.html just insert a cyclic Ajax get() request:

<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>

如果愿意,您甚至可以在进度计数器后面附加它.但是您需要注意防止线程多次运行.

You can even append this by a progress counter if you like. But you need to take care that you prevent the thread from being run multiple times.

这篇关于线程完成后,如何在Flask中更改渲染的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆