How does a python program launch another python program, as if it were starting from a separate SSH terminal?

Jessie runs on the Raspberry Pi 2. I have two displays, (by default) an HDMI display and an LCD touch screen (for which you need to set a couple of SDL-related variables using os.environ).

I have two pygame programs, lcd.py and hdmi.py, which, when launched from separate SSH terminals, coexist well, lcd.py dsiplays have several buttons, hdmi.py displays a slide show on the connected HDMI display.

If I run them separately in two SSH terminals (as the user “pi”, using sudo python PROGRAM), lcd.py is displayed on the LCD and hdmi.py displays the slideshow on the HDMI screen.

However, I cannot understand how lcd.py to call hdmi.py as a completely independent process (therefore, it has its own env variables and can control the HDMI display regardless of the parent process controlling the LCD).

The program lcd.pyhas a button that, when touched, calls the startSlideShow () procedure

However, various things that I tried to lcd.pystart in startSlideShow () hdmi.pydo not execute:

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?

Continuous interprocess communication is not required. In addition, when the lcd.py program is to “launch” the hdmi.py program, they do not need to communicate, and in fact it is not so important for me whether lcd completes the completion of the hdmi.py program.

Things I tried in startSlideShow () that do not work :

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

AND

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'

and

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

AND

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) 

AND

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

and

pid = os.spawnlp(os.P_NOWAIT, "/usr/bin/python",  "/home/pi/Desktop/code/hdmi.py")
# invokes python interpreter, get >>> on SSH terminal

AND

pid = os.spawnlp(os.P_NOWAIT, "/usr/bin/python /home/pi/Desktop/code/hdmi.py")
# creates zombie process [python] <defunct>
+4
1

, , env POpen . , , DISPLAY.

+2

Source: https://habr.com/ru/post/1625764/


All Articles