用python/flas刷新用户网页

2024-06-01 06:06:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个线程,线程完成后,我想刷新用户页面,并将他发送到另一个页面。但我对python和flask还是个新手,所以我不知道怎么做。在

这是我目前为止的代码:

nit = Thread()
def stampa():
    print ("Starting")
    com = "python plan.py"
    proc = subprocess.Popen(com.split(),shell=False)
    if proc.wait()!=0:
        print ("Ne radi")
    else:
        print ("Radi")
        return redirect('ended')


@app.route('/', methods=['GET','POST'])
def home():
    return render_template("homepage.html")

@app.route('/start_thread', methods=['GET','POST'])
def start_thread():
    global nit
    nit = Thread(target = stampa)
    nit.start()
    return redirect('end')

@app.route('/end', methods=['GET','POST'])
def end():
    global nit
    if nit.is_alive():
        return "Still working"
    else:
        return redirect('ended')
@app.route('/ended', methods=['GET','POST'])
def ended():
    return "It has ended"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

主页模板只有一个重定向到start_thread的按钮。 我启动一个线程的原因是我不想让windows在程序运行时冻结(大约需要5分钟才能完成)。现在用户必须手动刷新页面,以查看该过程是否已完成,但我希望能够自己完成。 有人有办法吗?(或任何我可以研究的想法?)在


Tags: appgetreturnifdef页面post线程
1条回答
网友
1楼 · 发布于 2024-06-01 06:06:52

As stated in the documentation, wait can cause deadlock, so communicate is advisable. subprocess.html#convenience-functions

请尝试一下

def stampa():
    print ("Starting")
    com = "python plan.py"
    proc = subprocess.Popen(com.split(),shell=False)
    if proc.wait()!=0:
        print ("Ne radi")
    else:
        print ("Radi")
        return redirect('ended')

^{pr2}$

相关问题 更多 >