I've had a look at the documentation for both of them.
This question is prompted by J.F.'s comment here: Retrieving the output of subprocess.call()
The current Python documentation for subprocess.call()
says the following about using PIPE
for subprocess.call()
:
Note Do not use
stdout=PIPE
orstderr=PIPE
with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.
Python 2.7 subprocess.call()
:
Note Do not use
stdout=PIPE
orstderr=PIPE
with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.
Python 2.6 includes no such warnings.
Also, the subprocess.call()
and subprocess.check_call()
don't seem to have a way to access their output, except for using stdout=PIPE with communicate():
https://docs.python.org/2.6/library/subprocess.html#convenience-functions
Note that if you want to send data to the process’s
stdin
, you need to create thePopen
object withstdin=PIPE
. Similarly, to get anything other than None in the result tuple, you need to givestdout=PIPE
and/orstderr=PIPE
too.
https://docs.python.org/2.6/library/subprocess.html#subprocess.Popen.communicate
What difference between subprocess.call()
and subprocess.Popen()
makes PIPE
less secure for subprocess.call()
?
More Specific: Why does subprocess.call()
"deadlock based on the child process output volume.", and not Popen()
?
Both
call
andPopen
provide means of accessing the output of your command:Popen
you can usecommunicate
or provide a file descriptor or file object to thestdout=...
parameter.call
your only option is to pass a file descriptor or a file object to thestdout=...
parameter (you can not usecommunicate
with this one).Now, the reason why
stdout=PIPE
is insecure when used withcall
is becausecall
doesn't return until the subprocess has finished, this means all the output would have to reside in memory until that moment, and if the amount of output is too much then that would fill the OS pipes' buffer.The references where you can validate the above information are the following:
call
andPopen
are the same:stdout
parameter are:call()
is justPopen().wait()
(± error handling).You should not use
stdout=PIPE
withcall()
because it does not read from the pipe and therefore the child process will hang as soon as it fills the corresponding OS pipe buffer. Here's a picture that shows how data flows incommand1 | command2
shell pipeline:It does not matter what your Python version is -- the pipe buffer (look at the picture) is outside of your Python process. Python 3 does not use C stdio but it affects only the internal buffering. When the internal buffer is flushed the data goes into the pipe. If
command2
(your parent Python program) does not read from the pipe thencommand1
(the child process e.g., started bycall()
) will hang as soon as the pipe buffer is full (pipe_size = fcntl(p.stdout, F_GETPIPE_SZ)
~65K on my Linux box (max value is/proc/sys/fs/pipe-max-size
~1M)).You may use
stdout=PIPE
if you read from the pipe later e.g., usingPopen.communicate()
method. You could also read fromprocess.stdout
(the file object that represents the pipe) directly.