What is the difference if I don't use stdout=subprocess.PIPE in subprocess.Popen()?
stdout=None
means, the stdout
-handle from the process is directly inherited from the parent, in easier words it basically means, it gets printed to the console (same applies for stderr
).
Then you have the option stderr=STDOUT
, this redirects stderr
to the stdout
, which means the output of stdout
and stderr
are forwarded to the same file handle.
If you set stdout=PIPE
, Python will redirect the data from the process to a new file handle, which can be accessed through p.stdout
(p
beeing a Popen
object). You would use this to capture the output of the process, or for the case of stdin
to send data (constantly) to stdin
.
But mostly you want to use p.communicate
, which allows you to send data to the process once (if you need to) and returns the complete stderr
and stdout
if the process is completed!
One more interesting fact, you can pass any file-object
to stdin/stderr/stdout
, e.g. also a file opened with open
(the object has to provide a fileno()
method).
To your wait
problem. This should not be the case! As workaround you could use p.poll()
to check if the process did exit! What is the return-value of the wait
call?
Furthermore, you should avoid shell=True
especially if you pass user-input as first argument, this could be used by a malicious user to exploit your program! Also it launches a shell process which means additional overhead. Of course there is the 1% of cases where you actually need shell=True
, I can't judge this with your minimalistic example.
stdout=None
means that subprocess prints to whatever place your script printsstdout=PIPE
means that subprocess' stdout is redirected to a pipe that you should read e.g., usingprocess.communicate()
to read all at once or usingprocess.stdout
object to read via a file/iterator interfaces