[Twisted-Python] Knowing whether spawnProcess did anything useful

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Jul 13 22:34:34 EDT 2010

On 13 Jul, 09:18 pm, dominic at baudvine.net wrote:
>I'm looking for a way to know whether spawnProcess() did anything
>useful.  That is, whether anything has actually been executed. In my
>specific case the executables are all persistent (pluggable modules,
>they stick around for a while) so based on the following snippet I
>thought I could check whether the resulting ProcessTransport has a pid:
>"From before IProcessProtocol.makeConnection is called to before
>IProcessProtocol.processEnded is called, pid is an int giving the
>platform process ID of this process. pid is None at all other 
>However, a quick tour on the REPL shows otherwise (than I expected, not
>necessarily otherwise than what the docs say):
>>>>from twisted.internet import reactor
>>>>from twisted.internet import protocol
>>>>p = protocol.ProcessProtocol()
>>>>f = reactor.spawnProcess(p, "fail", ["fail"])
><Process pid=21053 status=-1>
>I thought status might be of some relevance, but it didn't really 
>when I gave it a shot with actual executables.
>Something tells me I'm being massively stupid here, but I'm genuinely
>stuck.  Can't really believe that there's no way to do this, though.

Without the reactor running, neither the pid nor the status will ever be 
updated.  The event loop is needed in order to respond to the event of 
the child process exiting.

Also, the usual way to interact with the IProcessTransport provider (the 
Process instance) is via the ProcessProtocol instance.  Subclass it, 
override connectionMade or processExited or one of the other methods and 
wait for the reactor to call them.  Oh, and start up the reactor, too. 


More information about the Twisted-Python mailing list