[Twisted-Python] Sane way to get the exit code from reactor.spawnProcess(SomeProtocol, ...)

Nathan nathan.stocks at gmail.com
Tue Jun 24 18:43:42 EDT 2008

Is there a more sane way to get the exit code from a ProcessProtocol
than by parsing it out of reason.value in
ProcessProtocol.processEnded()?  I can't seem to find the error code
by itself anywhere. (see example code at bottom)

If I replace "print reason.value" with "print dir(reason)" below, then
I get the following output for both successful and unsuccessful

['__doc__', '__getstate__', '__init__', '__module__', '__repr__',
'__str__', '_findFailure', '_yieldOpcode', 'check', 'cleanFailure',
'count', 'frames', 'getBriefTraceback', 'getErrorMessage',
'getTraceback', 'getTracebackObject', 'parents', 'pickled',
'printBriefTraceback', 'printDetailedTraceback', 'printTraceback',
'raiseException', 'stack', 'tb', 'throwExceptionIntoGenerator',
'trap', 'type', 'value']

Most of the *Traceback functions include the "value" line inside them.
 getErrorMessage() apparently returns "value" itself.  "value" looks

"A process has ended without apparent errors: process finished with
exit code 0."


"A process has ended with a probable error condition: process ended
with exit code 1."

...but the actual exit code doesn't seep to appear (by itself) anywhere!

~ Nathan

from twisted.internet import reactor
from twisted.internet.protocol import ProcessProtocol

class TestProtocol(ProcessProtocol):
   def processEnded(self, reason):
      print reason.value

def good():
   good_binary = "/bin/ls"
   good_args = ("/bin/ls", '/')
   good_process = reactor.spawnProcess(TestProtocol(), good_binary,

def bad():
   bad_binary = "/bad"
   bad_args = ("/bad", '/')
   bad_process = reactor.spawnProcess(TestProtocol(), bad_binary, args=bad_args)

reactor.callLater(1.0, bad)
reactor.callLater(2.0, reactor.stop)

More information about the Twisted-Python mailing list