[英]how can python program run another python program as if it is being run from separate SSH terminal?
在運行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]<defunct> 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] <defunct>
為了給子進程提供與父進程不同的環境變量值,可以使用POpen
構造函數的env
參數並提供所需的值。 例如,這應該允許您提供不同的DISPLAY
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.