当我在生产模式下运行 Flask 应用程序时死循环

问题描述 投票:0回答:0

我有 Flask 应用程序。它有两个按钮开始和停止

from flask import Flask, render_template
import sys
flag1=True
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('index.html')

@app.route('/start/')
def start():
  globals()['flag1']=True
  while flag1==True:
    print('pStart')
  return render_template('index.html')

@app.route('/stop/')
def stop():
  globals()['flag1']=False
  return render_template('index.html')

if __name__ == '__main__':
  app.run(debug=True)

这是我的模板\index.html

<!doctype html>


<head><title>Test</title> 
    <meta charset=utf-8>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

    </head>
    <body>
        <h1>My Website</h1>
<a href="http://localhost:5000/start/">Start</a> 

<a href="http://localhost:5000/stop/">Stop</a> 
    
    </body>

此应用程序在开发模式下运行良好。然而,当我用 uWSGI 运行它时,我无法停止它(无限循环的 print('pStart'))。 这是我的 wsgi.py

from myproject import app

if __name__ == "__main__":
    app.run()

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

我尝试使用全局变量、线程、多处理来编写这个应用程序。我想在单击“停止”按钮时停止运行“print('pStart')”(当我使用 uWSGI 或任何其他服务器在生产模式下运行 Flask 应用程序时)。

python flask uwsgi
© www.soinside.com 2019 - 2024. All rights reserved.