How can I know how much to read from a subprocess

Karthik Gurusamy kar1107 at gmail.com
Tue Sep 18 19:44:58 EDT 2007


On Sep 17, 4:14 pm, spam.n... at gmail.com wrote:
> Hello,
>
> I want to write a terminal program in pygtk. It will run a subprocess,
> display everything it writes in its standard output and standard
> error, and let the user write text into its standard input.
>
> The question is, how can I know if the process wrote something to its
> output, and how much it wrote? I can't just call read(), since it will
> block my process.

The solution is not simple. You may try a separate thread to do the
reading (so that you can afford to block that single thread). You can
also run into other I/O deadlocks with a simple solution (ie if you
try to feed the whole stdin before reading any stdout, your stdin
feeding can block because the process's stdout is full --  it's a bit
confusing but for large stdin/stdout, a deadlock is very much
possible).

Did you try subprocess.Popen.communicate() ? Under the covers it
should take care of th ese deadlock issues. It may read and buffer the
whole stdout/stderr, so don't use it if the process generates infinite/
very-large stdout/stderr.

If the process you launch needs two way communication, like an ssh/ftp
session (where the stdin depends on process's prior stdout), the only
viable solution is to simulate a human (e.g. pexpect module)

Karthik
>
> Thanks,
> Noam





More information about the Python-list mailing list