[Twisted-Python] Knowing whether spawnProcess did anything useful
Hiya, 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 times."[1] 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"]) f <Process pid=21053 status=-1> f.pid 21053
I thought status might be of some relevance, but it didn't really change 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. Thanks. [1] http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... -- Dominic van Berkel "You don't have conversations with microprocessors. You tell them what to do, then helplessly watch the disaster when they take you literally." - David Brin, Startide Rising
On 13 Jul, 09:18 pm, dominic@baudvine.net wrote:
Hiya,
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 times."[1]
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"]) f <Process pid=21053 status=-1> f.pid 21053
I thought status might be of some relevance, but it didn't really change 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. :) Jean-Paul
On 14-7-2010 4:34, exarkun@twistedmatrix.com wrote:
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.
<snip> Knew I was forgetting something - starting the reactor would probably be a fine idea, yeah. The code I've got on file does all the subclassing and even starts the reactor, I just blindly forgot to start the reactor on the REPL. That's what I get for copying the base of my code from an example. Thanks! -- Dominic van Berkel "You don't have conversations with microprocessors. You tell them what to do, then helplessly watch the disaster when they take you literally." - David Brin, Startide Rising
participants (2)
-
Dominic van Berkel
-
exarkun@twistedmatrix.com