How to get exit code when using Python subprocess communicate method?
Solution 1
Popen.communicate
will set the returncode
attribute when it's done(*). Here's the relevant documentation section:
Popen.returncode
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).
So you can just do (I didn't test it but it should work):
import subprocess as sp
child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
streamdata = child.communicate()[0]
rc = child.returncode
(*) This happens because of the way it's implemented: after setting up threads to read the child's streams, it just calls wait
.
Solution 2
.poll()
will update the return code.
Try
child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
returnCode = child.poll()
In addition, after .poll()
is called the return code is available in the object as child.returncode
.
Solution 3
You should first make sure that the process has completed running and the return code has been read out using the .wait
method. This will return the code. If you want access to it later, it's stored as .returncode
in the Popen
object.
Solution 4
exitcode = data.wait()
. The child process will be blocked If it writes to standard output/error, and/or reads from standard input, and there are no peers.
Solution 5
Use process.wait()
after you call process.communicate()
.
For example:
import subprocess
process = subprocess.Popen(['ipconfig', '/all'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
exit_code = process.wait()
print(stdout, stderr, exit_code)
Related videos on Youtube
CarpeNoctem
Updated on February 24, 2022Comments
-
CarpeNoctem about 2 years
How do I retrieve the exit code when using Python's
subprocess
module and thecommunicate()
method?Relevant code:
import subprocess as sp data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]
Should I be doing this another way?
-
Mechanical snail over 10 years
.communicate()
already waits for the subprocess to terminate. -
uglycoyote over 7 yearsThis example helped me, but it would be nice if examples didn't do the "import subprocess as sp" pattern of importing something standard as an obscure abbreviation. While this trims 8 characters off the code that follows it, it also makes it difficult to understand and reuse.
-
Jason C over 7 years@uglycoyote There's no rule that says you have to copy and paste. Just retype it however you want, it's like 4 like lines.
-
Mitch over 7 years@uglycoyote you could also edit it to be something like
from subprocess import Popen
and then just usePopen
instead ofsubprocess(or sp).Popen
which I'd say probably increases readability and shortens lines -
WesternGun over 6 yearsYeah... must call
process.communicate()
and then assignreturncode
to some variable. If the assignment is done before callingcommunicate
, isNone
. -
Nisba about 6 yearsIs it possible to show the return code without redirecting the pipe? I am calling a bash code and I would like to see the output in real time in the terminal
-
NateW over 5 yearswhen I did this .poll() was empty. I had to run child.communicate() in the line above child.poll() for this to work.
-
gg99 about 5 yearsI think you meant to use .wait() instead of .poll(), as per documentation: docs.python.org/3/library/subprocess.html. Note that .wait() takes an optional timeout param which can be convenient.
-
Ryan McGrath over 2 years@Nisba, the redirection is not required.
-
tripleee about 2 yearsThis is an adaptation of my answer to a duplicate question.
-
tripleee about 2 yearsYou can't
communicate
afterwait
. -
tripleee about 2 yearsYou can't meaningfully
wait
more than once.