首页 > 解决方案 > 烧瓶网络服务器 | 停止 python 脚本会导致崩溃

问题描述

我正在运行一个带有树莓派 3B+ 的烧瓶网络服务器。html 网站有一些文本和两个按钮。一键运行外部 python 脚本(不是烧瓶 python 脚本),一键停止 python 脚本。

到目前为止,按下“开始”按钮可以让我的外部 python 脚本按照我想要的方式运行。效果很好,我对此没有任何抱怨

我希望我的“停止”按钮停止使用“开始”按钮开始运行的外部 python 脚本。该脚本中有一个永无止境的while循环。

我已经尝试了我能想到的一切:

这些都不起作用。它只会导致我的网络服务器崩溃并且我的外部脚本继续运行(大声笑)

我得到“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

标签: pythonhtmlflaskraspberry-piraspberry-pi3

解决方案


替代方案..使其成为守护进程。允许您执行操作系统系统调用,例如

python program.py stop
python program.py start
python program.py restart

一些相关信息。

在 Ubuntu 中将我的 Python 程序安装为服务

https://stackoverflow.com/a/21597436/14529821


推荐阅读