如何让python脚本不断运行?

时间:2016-05-04 22:15:43

标签: php python execute

我已阅读了一堆论坛但仍然没有运气..我有一个执行python文件的php文件(liveSensor.py)。它运行py文件一次并停止,如何在后台保持文件打开?如果我在命令行中运行sudo python liveSensor.py它也只运行一次但是如果我把sudo python -i liveSensor.py保持打开状态。如何在php中执行此操作?

php文件 -

<?php
$try = exec('python sensor.py');
print_r ($try);
?>

在liveSensor.py文件中,我有2个振动传感器被检测到,然后将该信息传递给sqlite,然后我的php文件检索该数据并使用ajax我可以显示它。除了不断启动py文件之外,所有这些都很有效。

py script -

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

KnockPin = 12 
ShockPin = 13 



inning = 1
runs = 0
strikes = 0
balls = 0

print ("Welcome to Bases Loaded")



def setstr():
    global strikes
    strikes = 0
    global balls
    balls = 0




def setup():
    GPIO.setmode(GPIO.BOARD)         
    GPIO.setup(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(ShockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)




def knock(ev=None):

with sqlite3.connect('basesLoaded.db') as conn:

    global strikes
    global inning


    strikes += 1

    if strikes ==0:

        pass
    elif strikes == 1:


        conn.execute("UPDATE bl set STRIKE = 1 WHERE ID =1");

        print ("Strike 1 :", conn.total_changes);

    elif strikes == 2:


        conn.execute("UPDATE bl set STRIKE = 2 WHERE ID=1");

        print ("Strike 2 :", conn.total_changes);

    elif strikes == 3:
        global inning
        inning +=1
        conn.execute("UPDATE bl set STRIKE = 0 WHERE ID=1");
        conn.execute("UPDATE bl set INNING = inning WHERE ID=1");

        print ("Strike 3 Your Out :", conn.total_changes);
        setstr()
        print ("Inning Number :", inning);

def shock(ev=None):
    with sqlite3.connect('basesLoaded.db') as conn:

        global balls

        balls += 1

        if balls ==0:
            pass
        elif balls == 1:

            conn.execute("UPDATE bl set BALL = 1 WHERE ID=1");
            conn.commit()
            print ("Ball 1 :", conn.total_changes);


        elif balls == 2:

            conn.execute("UPDATE bl set BALL = 2 WHERE ID=1");
            conn.commit()
            print ("Ball 2 :", conn.total_changes); 

        elif balls == 3:

            conn.execute("UPDATE bl set BALL = 3 WHERE ID=1");
            conn.commit()
            print ("Ball 3 :", conn.total_changes);

        elif balls == 4:

             global runs
            runs += 1
            conn.execute("UPDATE bl set BALL = 0 WHERE ID=1");
            conn.execute("UPDATE bl set RUN = 'runs' WHERE ID=1");
            conn.commit()
            print ("Run Scored! score is :", runs); 
            setstr()




def register_callbacks():
     GPIO.add_event_detect(ShockPin, GPIO.FALLING, callback=shock, bouncetime=2500)
     GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=knock, bouncetime=2500) 



if __name__ == '__main__':
    try:
        setup()
        register_callbacks()

    except KeyboardInterrupt: 
        destroy()  

这将返回我的“欢迎加载基地”#39;浏览器中的消息,但py文件不会保持打开状态以检测传感器。

1 个答案:

答案 0 :(得分:0)

对于这种交互,您需要构建一个在单独进程中运行并使用通信协议的服务器脚本,例如数据库。如果你考虑一下,PHP运行时无法控制python脚本:它可以运行它并为你提供输出,但PHP运行时如何知道你何时以及如何需要脚本的结果?

更好的方法是使用Python创建REST服务,按需提供传感器数据,并使用Javascript发出AJAX请求并根据需要获取数据。