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, 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., using process.communicate()
to read all at once or using process.stdout
object to read via a file/iterator interfacesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With