I recently noted in Python the subprocess.Popen()
has an argument:
stdout=None(default)
I also saw people using stdout=subprocess.PIPE.
What is the difference? Which one should I use?
Another question would be, why the wait() function can't wait until the process is really done sometimes? I used:
a = sp.Popen(....,shell=True)
a.wait()
a2 = sp.Popen(...,shell=True)
a2.wait()
sometimes the a2 command is executed before the command a is done.
stdout=None
means, thestdout
-handle from the process is directly inherited from the parent, in easier words it basically means, it gets printed to the console (same applies forstderr
).Then you have the option
stderr=STDOUT
, this redirectsstderr
to thestdout
, which means the output ofstdout
andstderr
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 throughp.stdout
(p
beeing aPopen
object). You would use this to capture the output of the process, or for the case ofstdin
to send data (constantly) tostdin
. But mostly you want to usep.communicate
, which allows you to send data to the process once (if you need to) and returns the completestderr
andstdout
if the process is completed!One more interesting fact, you can pass any
file-object
tostdin/stderr/stdout
, e.g. also a file opened withopen
(the object has to provide afileno()
method).To your
wait
problem. This should not be the case! As workaround you could usep.poll()
to check if the process did exit! What is the return-value of thewait
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 needshell=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