在运行Jessie的Raspberry Pi 2上,我有两个显示器,一个(默认)HDMI显示器和一个LCD触摸屏(需要使用os.environ设置几个与SDL相关的变量).
我有两个pygame程序,lcd.py和hdmi.py,当从不同的SSH终端运行时,很好地共存,lcd.py dsiplays几个按钮,hdmi.py在附加的HDMI显示器上显示幻灯片.
如果我在两个SSH终端中单独运行它们(作为’pi’用户,使用sudo python PROGRAM),lcd.py显示到LCD,hdmi.py显示到HDMI屏幕的幻灯片.
但是,我无法弄清楚如何让lcd.py调用hdmi.py程序作为一个完全独立的进程(因此它有自己的env变量,并且可以独立于驱动LCD显示器的父进程驱动HDMI显示).
lcd.py程序有一个按钮,触摸时调用例程startSlideShow()
但是,我在lcd.py startSlideShow()中尝试启动hdmi.py的各种事情都失败了:
def startSlideShow():
# when running in SSH the correct command is
# sudo python /home/pi/Desktop/code/hdmi.py
# or sudo /usr/bin/python /home/pi/Desktop/code/hdmi.py
# tried various ways of invoking hdmi.py via
# os.fork(), os.spawnl(), subprocess.Popen()
WHAT GOES HERE?
不需要进行持续的进程间通信.除了当lcd.py程序需要“启动”hdmi.py程序时,它们不需要进行通信,对我来说,终止lcd是否终止hdmi.py程序并不重要.
我在startSlideShow()中尝试的东西不起作用:
cmd = "sudo /usr/bin/python /home/pi/Desktop/code/hdmi.py"
pid = os.spawnl(os.P_NOWAIT, cmd)
# lcd.py keeps running, but creates a zombie process [python] instead of running hdmi.py
和
cmd = "sudo /usr/bin/python /home/pi/Desktop/code/hdmi.py"
pid = os.spawnl(os.P_DETACH, cmd)
# lcd.py exits with error AttributeError: 'module' object has no attribute 'P_DETACH'
和
cmd = "sudo /usr/bin/python /home/pi/Desktop/code/hdmi.py"
pid = os.spawnl(os.P_WAIT, cmd)
# no error message, returns a pid of 127, but does nothing, and no such process exists when I run `ps aux` in another SSH terminal
和
cmd = ["/usr/bin/python", "/home/pi/Desktop/code/hdmi.py" ]
pid = subprocess.Popen(cmd, stdout=subprocess.PIPE).pid # call subprocess
# runs the hdmi.py program, but output goes to LCD not HDMI
# (the 2 programs alternately take over the same screen)
和
cmd = ["/usr/bin/python", "/home/pi/Desktop/code/hdmi.py" ]
pid = subprocess.Popen(cmd).pid
# as above, both programs run on same display, which flashes between the two programs
和
pid = os.spawnlp(os.P_NOWAIT, "/usr/bin/python", "/home/pi/Desktop/code/hdmi.py")
# invokes python interpreter, get >>> on SSH terminal
和
pid = os.spawnlp(os.P_NOWAIT, "/usr/bin/python /home/pi/Desktop/code/hdmi.py")
# creates zombie process [python]
最佳答案 为了给子进程提供与父进程不同的环境变量值,可以使用POP构造函数的env参数并提供所需的值.例如,这应该允许您提供不同的DISPLAY值.