Getting both PID and output from a command

Hugh Macdonald HughMacdonald at brokenpipefilms.com
Sat Mar 20 05:33:31 EST 2004


I suspect I'll probably use Donn Cave's suggestion when I give it a go on
monday - I'd rather not use any external modules if I can help it, and I
know that stderr gives me output in a format that I can read (I know exactly
which command I want to run here, and I have the program working properly
except for not being able to stop if before it finishes on its own.....)

Hugh Macdonald

----- Original Message ----- 
From: "Noah" <noah at noah.org>
Subject: Re: Getting both PID and output from a command


> The Pexpect library lets you run external commands.
> You get output from a command and you can get the pid.
>     http://pexpect.sourceforge.net/
> You should be able to write code like this:
>
> import pexpect
> child = pexpect.spawn (command.split()[0], command.split())
> print child.pid
> try:
>     print child.read()
> except pexpect.TIMEOUT:
>     child.kill (9)
>
> One problem is that the stdout and stderr are merged into a single stream.
> This is a limitation of the Python pty library. Oops...
> You will still be able to read the error, but you can't read it
> separately from stdout.
>
> Note, I wouldn't trust using a pipe. You will not see any data on the pipe
> until the child decides to flush the pipe. There is no way to force the
> child to flush it's stdout (your stdin from your point of view).
> Pipes are bad for working with child apps that use the stdio libary.
> You need a pty for that.
>
> Yours,
> Noah
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list