dongyun51582 2019-05-08 23:24
浏览 94

如何使用php激活python脚本,而不是在time.sleep进行时加载页面

Using php and python I want to activate a python script, zone_on.py. My code works fine but whenever the php if statement is set my page loads and is not useable for the entire length of the python script. I want the script to be activated then the php goes back to working while the python script executes. Here is my php if statement:

$zone_on = $_GET['on'];
if (isset($zone_on)){
  $command = escapeshellcmd("python zone_on.py --zone $zone_on");
  shell_exec($command);
  // echo $output;

} 

and here is my python script named zone_on.py

import time
import argparse
import mysql.connector


number_of_zones = 7
ap = argparse.ArgumentParser()
ap.add_argument("-z", "--zone", type=str, required=True,
    help="zone number to turn on")
args = vars(ap.parse_args())



zone = [8, 7, 10, 9, 14, 15, 18, 23, 24, 25]


#time to run each zone
zone1_time = 100
zone2_time = 2
zone3_time = 2
zone4_time = 2
zone5_time = 2
zone6_time = 2


zone1  = [zone[0], zone1_time, 1]

zone2  = [zone[1], zone2_time, 2]

zone3 = [zone[2], zone3_time, 3]

zone4 = [zone[3], zone4_time, 4]

zone5 = [zone[4], zone5_time, 5]

zone6 = [zone[5], zone6_time, 6]



zone_num = args["zone"]

def openCloseValves(valve, zone_time, zone_num):
        print "Opening Zone", zone_num,"for",zone_time,"seconds"
        time.sleep(1.0)
        # GPIO.setup(valve, GPIO.OUT)
        print("Open")# GPIO.output(valve, GPIO.LOW)    # Open valve
        time.sleep(zone_time)
        print("Closed")
        # GPIO.output(valve, GPIO.HIGH)   # Close valve
        time.sleep(1.0)


def run_sprinklers():
    if zone_num == "1":
        openCloseValves(zone1[0],zone1[1], zone1[2])
    if zone_num == "2":
        openCloseValves(zone2[0],zone2[1], zone2[2])
    if zone_num == "3":
        openCloseValves(zone3[0],zone3[1], zone3[2])
    if zone_num == "4":
        openCloseValves(zone4[0],zone4[1], zone4[2])
    if zone_num == "5":
        openCloseValves(zone5[0],zone5[1], zone5[2])
    if zone_num == "6":
        openCloseValves(zone6[0],zone6[1], zone6[2])

run_sprinklers()
  • 写回答

1条回答 默认 最新

  • drzil26260 2019-05-09 00:09
    关注

    by default exec (shell_exec($command);) is blocking, to make it run in the background you have to deal with the output assuming you don't want it, we just throw it into the void (null)

    shell_exec("$command > /dev/null 2>/dev/null &");
    
    评论

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?