我的穿线功能导致烧瓶挂起

时间:2018-01-29 14:25:31

标签: python multithreading flask python-multithreading

我对Flask很新,所以请耐心等待。

所以我正在使用一个应用程序处理一个需要30分钟才能完成的功能(刻录测试QA套件)。因为这需要很长时间,我想让应用程序启动一个" loading ..."屏幕,因此用户不会只是盯着悬挂的站点30分钟。我做了一些搜索并找到了thread。我以自己的方式设置(仅显示必要的功能)

from flask import Flask, render_template, request
import subprocess
import tests
from threading import Thread

app = Flask(__name__)

def async_slow_function(test, arguments):
    thr = Thread(target=test, args=arguments)
    thr.start()
    print("Thread starting...")
    return thr

@app.route('/strobe')
def strobe():
    print(async_slow_function(tests.strobe, ""))
    return index()

if __name__ == '__main__':
    app.run(debug=True, threaded=True, host='0.0.0.0')

但是,对我来说这个设置仍然会在测试运行时挂起应用程序。即使测试完成,应用仍然会挂起。这让我相信线程仍在运行。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

更新 万一将来有人遇到与我相同的问题,以下是我的更新代码有效。

from flask import Flask, render_template, request
import subprocess
import tests
from threading import Thread

app = Flask(__name__)

def async_slow_function(test, argue):
    if argue != None:
        thr = Thread(target=test, args=[argue])
    else:
        thr = Thread(target=test)
    thr.start()
    return thr

@app.route('/strobe')
def strobe():
    async_slow_function(tests.strobe, None)
    return render_template('testing.html')

@app.route('/fade')
def fade(): 
    async_slow_function(tests.fade, None)
    return render_template('testing.html')

if __name__ == '__main__':
    app.run(threaded=True, host='0.0.0.0')

这与我返回index()函数有关。相反,我只是渲染了测试模板。

相关问题