popenX() misunderstanding on win32

Paul Moore gustav at morpheus.demon.co.uk
Tue May 21 11:10:24 EDT 2002


Steve Holden <sholden at holdenweb.com> writes:

> "Jon Nicoll" <jkn at nicorp.f9.co.uk> wrote ...
> > Then, how can I use popenX() to invoke the program, and write and read
> > to 'stdin' and 'stdout', to duplicate the above? my experiments so
> > far, involving os.popenX() and win32pipe.popenX(), haven't got me
> > anywhere - I either get nothing when I do a read, or the process hangs
> > waiting for input that I think should alread be there.
> 
> The synchronisation of input and output via pipes from a single process can
> be very tricky to solve if you don't know what to expect from the program.
> Even when you do, you will find that the OS's buffering system can get in
> your way. This is a classic problem in half-duplex protocol design.

Some suggestions:

1. flush the file descriptors frequently to make sure you don't have
   stuff in your stdio buffers.
2. make sure you write whole lines, with newlines at the end, before
   reading data back.

If all else fails, you can check the output pipe to see if the program
has produced data or is still waiting for more input using
PeekNamedPipe (which works on anonymous pipes, too, despite the
name). I have an example at home, but it's quite complicated.

I suspect that flush() is probably all you need in practice. But this
sort of thing *is* tricky, and you can't guarantee to avoid
deadlocks...

Paul.




More information about the Python-list mailing list