我的线程函数导致烧瓶挂起
My threading function causes flask to hang
我是 Flask 的新手,所以请多多包涵。
所以我正在使用一个应用程序来处理需要 30 分钟才能完成的功能(QA 套件的烧录测试)。因为这需要很长时间,所以我想让应用程序启动一个 "loading..." 屏幕,这样用户就不会仅仅盯着一个挂起的网站 30 分钟。我四处搜索了一下,发现了这个 。这是我自己设置的(只显示必要的功能)
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')
但是,我的这个设置在测试 运行 时应用程序仍然挂起。即使测试完成,应用程序仍然挂起。这让我相信线程仍然是 运行.
有什么想法吗?
更新
以防万一将来有人遇到与我相同的问题,下面是我更新后的有效代码。
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()函数有关。相反,我只是渲染了我的测试模板。
我是 Flask 的新手,所以请多多包涵。
所以我正在使用一个应用程序来处理需要 30 分钟才能完成的功能(QA 套件的烧录测试)。因为这需要很长时间,所以我想让应用程序启动一个 "loading..." 屏幕,这样用户就不会仅仅盯着一个挂起的网站 30 分钟。我四处搜索了一下,发现了这个
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')
但是,我的这个设置在测试 运行 时应用程序仍然挂起。即使测试完成,应用程序仍然挂起。这让我相信线程仍然是 运行.
有什么想法吗?
更新 以防万一将来有人遇到与我相同的问题,下面是我更新后的有效代码。
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()函数有关。相反,我只是渲染了我的测试模板。