python-3.xpython-2.7subprocesspopenomxplayer

understanding of subprocess, POPEN and PIPE


I am new to python and programming and I am trying to understand this code. I have spent the past few hours reading documentation and watching videos on subprocessing but I am still confused (I added snidbits of information of what I found online to comment the code as best I could).

Here are some questions I have pertaining to the code below:

when is subprocess used? when should I use Popen verses the more convenient handles with subprocess? what does PIPE do? what does close_fds do?

basically I need this line of code explained

my_process=Popen(['player',my_video_File_path], stdin=PIPE, close_fds=True)

full code here:

#run UNIX commands we need to create a subprocess
from subprocess import Popen, PIPE
import os
import time
import RPi.GPIO as GPIO
my_video_file_path='/home/pi/green1.mp4'
#stdin listens for information
# PIPE connnects the stdin with stdout
#pipe, (like a pipe sending info through a tunnel from one place to another )
#STDIN (channel 0):
#Where your command draws the input from. If you don’t specify anything special this will be your keyboard input.
#STDOUT (channel 1):
#Where your command’s output is sent to. If you don’t specify anything special the output is displayed in your shell.
#to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE.
#Popen interface can be used directly with subprocess

# with pipe The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.
#If we pass everything as a string, then our command is passed to the shell;
#"On Unix, if args is a string, the string is interpreted as the name or path of the program to execute. "
my_process=Popen(['player',my_video_File_path], stdin=PIPE, close_fds=True)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)


while True:
    button_state=GPIO.input(17)
    button_state1=GPIO.input(22)

    if button_state==False:
        print("quite video")
        my_process.stdin.write("q")
        time.sleep(.09)

    if button_state1==False:
        print("full video")
        my_process.stdin.write("fs")
        time.sleep(5)

Solution

  • Regarding the difference between subprocess and Popen, here is a line from the python 3.5 docs:

    For more advanced use cases, the underlying Popen interface can be used directly. (compared to using subprocess)

    So, this means that subprocess is a wrapper on Popen.

    PIPE is used so that your python script communicate with the subprocess via standard IO operations (you can think of print in python as a type of stdout).

    So, when you do my_process.stdin.write("fs") you are sending this text (or piping it) to the standard input of your subprocess. Then your subprocess reads this text and does whatever processing it needs to do.

    To further understand subprocessing, try to read standard input into a python program to see how it works. You can follow this How do you read from stdin in Python? question to work on this exercise.

    Also, it might be worthwhile to learn about piping in the more general linux style. Try to read through this article.