[Twisted-Python] [newb] another process question
Using this code: ... def processEnded(self, reason): print "processEnded, task %s, status %s" % (self.task, reason.value.exitCode,) Here is what I get from a normally terminated process processEnded, task ['sleep', '4']31662, status 0 But now I kill the subprocess, with SIGTERM, and I get: processEnded, task ['sleep', '15']31663, status None This seems to disagree with the twisted process doc. Instead of an exitCode, I seem to have gotten 'None' .processEnded(status): This is called when the child process has been reaped, and receives information about the process' exit status. The status is passed in the form of a Failure instance, created with a .value that either holds a ProcessDone object if the process terminated normally (it died of natural causes instead of receiving a signal, and if the exit code was 0), or a ProcessTerminated object (with an .exitCode attribute) if something went wrong. This scheme may seem a bit weird, but I trust that it proves useful when dealing with exceptions that occur in asynchronous code.
On Fri, Nov 05, 2010 at 07:28:24PM -0400, Neal Becker wrote:
The status is passed in the form of a Failure instance, created with a .value that either holds a ProcessDone object if the process terminated normally (it died of natural causes instead of receiving a signal, and if the exit code was 0), or a ProcessTerminated object (with an .exitCode attribute) if something went wrong.
The deal is, there are multiple kinds of 'went wrong' in the Unix process model. One kind (described the documentation you quote above) is a non-zero exit code; you can test this by running "/bin/false" rather than "sleep 15" as your test command. Another kind is 'killed by a signal', in which case the value "returned" by the process encodes the number of the signal that killed it, rather than a process-defined error code. Looking at the source of the ProcessTerminated class mentioned: http://twistedmatrix.com/trac/browser/trunk/twisted/internet/error.py#L268 ...it looks like killing a process with a signal puts 'None' in the .exitCode property, and the signal number in the .signal property. The instance also has a useful str() representation if you just want to log a sensible error message.
participants (2)
-
Neal Becker
-
Tim Allen