correct way of running a sub-process

Donn Cave donn at
Thu Feb 12 21:38:57 CET 2004

In article <c0gb41$1bl$1 at news.Stanford.EDU>,
 "Daniel Danger Bentley" <dbentley at> wrote:

> Correct me if I'm wrong, but can't this not catch all the output?  Or is
> read in python guaranteed to return all the data that can ever be returned
> (unlike the C library function)?

You are indeed wrong, misled by the name - the C library function
in question is fread(3), which does read all.  Not like the read(2)
system call, which is (a.k.a.

Back to the original question, I think the simplest way to get
status and output is

   fp = os.popen(cmd, 'r')
   output =
   status = os.WEXITSTATUS(fp.close())

Now if you want a timeout, you'll have to do it yourself, and
of course the file object isn't the way to go because of its
underlying buffered read.  You can turn off buffering, but then
you're looking at one system call per byte to implement readline
et al., so it's not a generally attractive solution.  Better to
get the file descriptor (fileobject.fileno()), and probably use
select on it.  You might also want to do that if you end up
reading from two different pipes for stderr and stdout, because
that can lead to the same buffer deadlock you're running into
now - if I'm reading from stdout while the process writes to
stderr, or vice versa, it can fill the pipe and block waiting
for me to empty it, which I won't do because I'm stuck on stdout.

   Donn Cave, donn at

More information about the Python-list mailing list