doujia7779 2016-05-04 22:15
浏览 246

如何让python脚本不断运行?

I have read through a bunch of forums and still no luck.. I have a php file that executes a python file (liveSensor.py). It runs the py file once and stops, how to I keep the file open in the background? If I run sudo python liveSensor.py in the command line it just runs once as well but if I put sudo python -i liveSensor.py it stays open. How to I do this in php?

php file -

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

In the liveSensor.py file I have 2 vibration sensors that are being detected and then passing that info into sqlite, then my php file retrieves that data and using ajax I can display it. All of this works great except launching the py file continually..

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()  

This returns my 'Welcome to Bases Loaded' message in the browser but the py file wont stay open to detect the sensors.

  • 写回答

1条回答

  • dongyuan7981 2016-05-04 22:26
    关注

    For that kind of interaction you need to build a server script that runs in a separate process and uses a communication protocol, like a database for instance. If you think about it, the PHP runtime has no control about the python script: it can run it and provide you with the output, but how is the PHP runtime going to know when and how you may need results from the script?

    A better approach would be to make a REST service with Python that provides the sensor data on demand and use Javascript to make AJAX requests and get the data as needed.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝