我是python的新手,需要一些帮助来完成一个项目,请让我说这不是学校的工作。我有一个 for 循环,用于控制树莓派上的 gpio 引脚,该文件的名称为"ledtest.py",该脚本是使用 php 中的"exec"命令执行的,当我按下页面上的按钮时,它会执行"ledtest.py"脚本,所有工作正常,我的 led 点亮并按照程序告诉它的方式操作, 我的问题是;如果循环运行太长,我有时想停止循环,我想在一个新的 python 文件上放置一个命令来停止循环,并在按下第二个按钮时从 php 执行它,我听说过 break 命令但不知道如何实现它任何帮助将不胜感激。
while not os.path.exists("/home/pi/stop_LEDS"):
do_blink_led()
os.remove("/home/pi/stop_LEDS")
然后在 PHP 中只需创建该文件...当您希望它停止时
你可以让它存在<?php exec('touch /home/pi/stop_LEDS');?>
试试这个
索引.php
<?php
if isset($_POST["stop"]){
// create the file that will exit the ledtest loop
exec('touch /home/pi/stop_LEDS');
echo '<form method="POST"><input type="submit" name="start" value="start" /></form>';
}else if isset($_POST["start"]){
// start ledtest
exec("ledtest.py");
echo '<form method="POST"><input type="submit" name="stop" value="stop" /></form>';
}else{
echo '<form method="POST"><input type="submit" name="start" value="start" /></form>';
}
?>
ledtest.py
while not os.path.exists("/home/pi/stop_LEDS"):
do_blink_led() # this is your method that you wrote that blinks the led
os.remove("/home/pi/stop_LEDS")