With the code below, p.returncode
is always None
. According to the Popen.returncode documentation, this means that the process hasn\'t finished yet
According to the linked to documentation
The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.
A negative value -N indicates that the child was terminated by signal N (Unix only).
You have not called poll
or wait
so returncode
won't be set.
On the other hand if you look in to the source code for fx check_output
you see that they are directly using the return value from poll
to examine the return code. They know that the process has terminated at that point because they have called wait
earlier. If you don't know that you would have to call the wait
method instead (but note the deadlock possibility noted in the documentation).
Normally the program will have terminated when you've read all of stdout/stderr, but that's not guaranteed and that may be what you're seeing. Either the program or the OS can close stdout
(and stderr
) before the process actually terminates and then by just calling poll
immediately after you've read all the output from the program might fail.