从PHP调用无尽的Python脚本 [英] Calling a endless Python script from PHP

查看:240
本文介绍了从PHP调用无尽的Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用python脚本的PHP脚本.两者都在同一台Linux服务器上运行.

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

Python脚本在"while true"循环中运行.现在,当我启动PHP脚本时,它仍然处于无休止的循环中,并且永无止境.

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.

如果我在Python中删除循环,则PHP运行正常.

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)

如何使用PHP使用无限循环正确启动Python脚本?

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

推荐答案

好的,一种选择是包括Linux'&'在您的shell_exec()函数中.这使命令在后台运行,但是您无法从脚本内(轻松)停止它. 然后,代码变成(请注意'&'):

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>

这将使脚本永久在后台运行,或者至少在重新启动Pi之前.

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

这篇关于从PHP调用无尽的Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆