correct way of running a sub-process
Jp Calderone
exarkun at intarweb.us
Thu Feb 12 10:08:33 EST 2004
On Thu, Feb 12, 2004 at 06:10:01AM -0800, Daniel Timothy Bentley wrote:
> What is the (hopefully unique) obvious way of runnign a sub-process if I
> want to get the exit code and input without resorting to multi-threading?
>
> It seems like I should be able to do the following:
>
> foo = popen2.Popen3(cmd)
> foo.wait()
> foo.fromchild.read()
>
> But, it seems like on my system (a Sun Ultra 30 running Solaris 8) this
> will hang if the input happens to have more than about 12k of text. I'm
> guessing this is a buffersize issue, but it would be a mistake to assume
> at compile time an output size for the child process.
>
> Couldn't wait be modified to actually wait until a process exits by
> buffering?
>
> While we're at it, is there any way to read/wait with a time-out? I.e.,
> read more than 0 bytes, unless it takes more than a time-out value, so I
> can run sub-processes that terminate?
>
(Untested)
from twisted.internet import protocol, reactor
class PrintyProtocol(protocol.ProcessProtocol):
bytes = ''
errBytes = ''
def outReceived(self, bytes):
self.bytes += bytes
def errReceived(self, bytes):
self.errBytes += bytes
def processEnded(self, reason):
print 'Process done. Got:', repr(self.bytes), repr(self.errBytes)
reactor.stop()
reactor.spawnProcess(PrintyProtocol(), cmd, args=(cmd,))
reactor.run()
Jp
More information about the Python-list
mailing list