[Twisted-Python] ProcessProtocol randomly not returning all of stdout
Justin Johnson
justinjohnson at gmail.com
Thu Jan 13 14:47:05 EST 2005
I recently started running into a problem where my process protocol
isn't extracting stdout accurately in all cases. I have a method on a
service that loops through a list and runs a command, passing in each
item on the list one at a time. Out of ~50 items in the list, and
thus ~50 invocations of the command, one or two will fail to extract
all of stdout from the command. Instead the first character of stdout
is extracted and that is it.
The process protocol I'm using is below. Does anyone see anything
wrong with the code? I am using the win32event reactor and Twisted
1.3.0.
from twisted.internet import reactor
from twisted.internet import protocol, error, defer
from twisted.python import log
from ratcontrol import errors, config
import os, sys
class BasicProcessProtocol(protocol.ProcessProtocol):
def __init__(self):
self.deferred = defer.Deferred()
self.stdout = ""
self.stderr = ""
self.status = 0
def connectionMade(self):
self.transport.closeStdin()
def outReceived(self, data):
self.stdout += data
def errReceived(self, data):
self.stderr += data
def processEnded(self, reason):
if reason.check(error.ProcessDone):
self.status = 0
# pass the items we want down the callback chain
self.deferred.callback((self.status, self.stdout, self.stderr))
else:
self.status = 1
theError = errors.CmdError(self.cmd, self.status,
self.stdout, self.stderr)
log.msg(theError)
self.deferred.errback(theError)
def openProcess(cmd_and_args):
cmd = cmd_and_args[0]
args = cmd_and_args[1:]
# Spawn the process using the reactor so we don't block
proto = BasicProcessProtocol()
proto.cmd = cmd_and_args
reactor.spawnProcess(proto, cmd, cmd_and_args, env=os.environ)
return proto.deferred
More information about the Twisted-Python
mailing list