Dialog with a process via subprocess.Popen blocks forever

Hendrik van Rooyen mail at microcorp.co.za
Fri Mar 2 01:16:36 EST 2007


 <bayer.justin at googlemail.com> wrote:

8<------------------
> The C programm gets its "commands" from its stdin and sends its state
> to stdout. Thus I have some kind of dialog over stdin.
> 
> So, once I start the C Program from the shell, I immediately get its
> output in my terminal. If I start it from a subprocess in python and
> use python's sys.stdin/sys.stdout as the subprocess' stdout/stdin I
> also get it immediately.

so why don't you just write to your stdout and read from your stdin?

> 
> BUT If I use PIPE for both (so I can .write() on the stdin and .read()

This confuses me - I assume you mean write to the c program's stdin?

> from the subprocess' stdout stream (better: file descriptor)) reading
> from the subprocess stdout blocks forever. If I write something onto
> the subprocess' stdin that causes it to somehow proceed, I can read
> from its stdout.

This sounds like the c program is getting stuck waiting for input...

> 
> Thus a useful dialogue is not possible.
> 

If you are both waiting for input, you have a Mexican standoff...

And if you are using threads, and you have issued a .read() on
a file, then a .write() to the same file, even followed by a .flush()
will not complete until after the completion of the .read().

So in such a case you have to unblock the file, and do the .read() in
a try - except clause, to "free up" the "file driver" so that the .write()
can complete.

But I am not sure if this is in fact your problem, or if it is just normal
synchronisation hassles...

- Hendrik




More information about the Python-list mailing list