代码之家  ›  专栏  ›  技术社区  ›  eonmax

Lua在后台执行python脚本

  •  1
  • eonmax  · 技术社区  · 7 年前

    我在后台运行脚本时遇到问题。

    我有Lua文件:

    function on_msg_receive (msg)
      if (msg.text=="Alarmon") then
        send_msg (msg.from.print_name, 'Sensor ON!', ok_cb, false)
        io.popen('/home/pi/led.py')
      end
    end
    

    Python文件(用于测试):

    import RPi.GPIO as GPIO
    import time
    pinn=4
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(pinn,GPIO.OUT)
    print "LED on"
    GPIO.output(pinn,GPIO.HIGH)
    time.sleep(5)
    print "LED off"
    GPIO.output(pinn,GPIO.LOW)
    GPIO.cleanup()
    

    执行后,我得到:

    /home/pi/led.py: 1: /home/pi/led.py: import: not found
    /home/pi/led.py: 2: /home/pi/led.py: import: not found
    /home/pi/led.py: 4: /home/pi/led.py: Syntax error: word unexpected (expecting ")")
    

    它的Lua文件用于处理电报。我运行消息“Alarmon”,然后它一直工作到错误弹出为止。 我认为有一个问题 io.popen 不要跑 led.py 在python中。 如何更改?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Piglet    7 年前

    在代码中,您只提供io。弹出Python脚本的路径。

    function on_msg_receive (msg)
      if (msg.text=="Alarmon") then
        send_msg (msg.from.print_name, 'Sensor ON!', ok_cb, false)
        io.popen('/home/pi/led.py')
      end
    

    你的电脑不知道该怎么处理它。

    Lua 5.3 Reference Manual :

    io。popen(程序[,模式])

    此功能取决于系统,并非所有 平台。

    在单独的进程中启动程序prog并返回文件句柄 可用于从此程序读取数据(如果模式为“r”,则 默认)或将数据写入此程序(如果模式为“w”)。

    因此,为了使其工作,您必须实际启动一个程序,在您的情况下,它就是Python解释器。 假设您在PATH系统变量中有Python解释器的位置,那么应该可以:

    io.popen('Python')
    

    当您希望interpeter运行脚本时,您还可以提供脚本的路径作为参数。

    io.popen('Python /home/pi/led.py')
    

    与您在命令行界面中输入的内容相同。。。

    如果您不打算在程序中使用任何输入或输出,您可以简单地使用:

    os.execute('Python /home/pi/led.py')
    

    相反