python - 烧瓶网络服务器 | 停止 python 脚本会导致崩溃
问题描述
我正在运行一个带有树莓派 3B+ 的烧瓶网络服务器。html 网站有一些文本和两个按钮。一键运行外部 python 脚本(不是烧瓶 python 脚本),一键停止 python 脚本。
到目前为止,按下“开始”按钮可以让我的外部 python 脚本按照我想要的方式运行。效果很好,我对此没有任何抱怨
我希望我的“停止”按钮停止使用“开始”按钮开始运行的外部 python 脚本。该脚本中有一个永无止境的while循环。
我已经尝试了我能想到的一切:
- 创建一个“正在运行”的布尔变量,以便 while 循环仅在该变量等于 true 时运行,然后使用停止按钮更改该变量。
- 在按下“停止”按钮时调用的外部脚本中有一个“exit()”函数。
这些都不起作用。它只会导致我的网络服务器崩溃并且我的外部脚本继续运行(大声笑)
我得到“TypeError:视图函数没有返回有效响应。”
我的烧瓶代码:
from flask import Flask, render_template
import miniMSA_Version2 as MSA
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/run')
def run_MSA():
return MSA.run()
@app.route('/stop')
def stop_msa():
return MSA.stop()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=True)
我的html代码:
<!doctype html>
<head>
<title> MSA Webpage </title>
<meta charset=utf-8>
</head>
<body>
<h1> heading </h1>
<h2> 2nd heading </h2>
<p> cool paragraph </p>
<button> <a href="/run"> Start </a> </button>
<button> <a href="/stop"> Stop </a> </button>
</body>
外部 Python 脚本(注意:这是我的外部脚本的简化版本,它超过 170 行长,我无法复制和粘贴它,因为我的 Raspberry pi 没有互联网,所以是的,我复制了所有代码在我的帖子中手动):
import some stuff
import some more stuff
def run():
#create output file name etc
def get_data():
# run, collect and append data to output file
while True:
#flash led code here
# call get_data() function
解决方案
替代方案..使其成为守护进程。允许您执行操作系统系统调用,例如
python program.py stop
python program.py start
python program.py restart
一些相关信息。
推荐阅读
- javascript - Infragistics - show/hide columns in code (not using default column chooser dialog)
- reactjs - 在反应中创建高图漏斗图的问题
- php - 页面不工作,太多次
- java - Spring Boot 测试启动应用程序
- python - 从 python 运行时如何忽略 Excel VBA 错误(对话框)
- node.js - Mongoose 填充排序选项
- angular - webpack 没有显示字体很棒的图标(只有空方块)
- material-ui - material-ui onDblClick 表格行
- reactjs - 开玩笑如何在 package.json 脚本中设置文件模式
- python - 在 python 中打印数字 1 到 n 的总和