dongyin5516 2014-02-09 15:29
浏览 24
已采纳

从PHP调用无尽的Python脚本

I have a PHP script that calls a python script. Both running on the same Linux server.

The Python script is running in a "while true" loop. Now when I start the PHP script, it remains in an endless loop and never ends.

If i delete the loop in Python, PHP is running normaly.

PHP:

<html>
 <head>
   <title>PHP</title>
 </head>
 <body>
  <?php 
      shell_exec('sudo python /home/pi/blink.py 1); 
  ?> 
 </body>
</html>

Python:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sys

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()

GPIO.setup(4, GPIO.OUT)

def blink(self):
    while True:
        time.sleep(0.5);
        GPIO.output(4, GPIO.LOW)
        time.sleep(0.5);
        GPIO.output(4, GPIO.HIGH)

if  str(sys.argv[1]) is '1':
     blink("")
else:
     GPIO.output(4, GPIO.LOW)

Edit: How do I properly start a Python script with an infinite loop using PHP?

  • 写回答

3条回答 默认 最新

  • duannuo7878 2014-02-09 15:36
    关注

    Alright, one option is to include the Linux '&' in your shell_exec() function. This makes the command run in the background, you can't stop it (easily) from within the script though. Code then becomes (note the '&'):

    <html>
     <head>
       <title>PHP</title>
     </head>
     <body>
      <?php 
          shell_exec('sudo python /home/pi/blink.py 1 &'); 
      ?> 
     </body>
    </html>
    

    This makes the script run in the background forever, or at least until the Pi is rebooted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮