read and readline hanging

Olivier Lefevre lefevrol at yahoo.com
Fri Jan 25 17:31:16 CET 2008


Hi Steve,

Thanks for the answer. Yes this is tricky. I have done it in Java
before, where you can, e.g., set up a thread to pump stuff out of
both stderr and stdout continuously but my python is too rudimentary
for that. There is a key difference anyway: in Java you can write

     while (br.readLine() != null) { <pump> }

where br is the buffered reader in which you've wrapped the stdout
of the child process and it will not hang. But in python eventually
stdout.readline() hangs. This is a real nuisance: why can't it just
return None?

> 1. The subprocess has stopped producing output.

Indeed, if I do this interactively, I can tell after 3 lines that I've
gotten all there is to get right now and the fourth readline() call
hangs. But how can I find out *programmatically* that there is no more
input?

> If you are only reading its standard output, are you sure that the 
 > subprocess is flushing its buffers so you can recognize it's time to
 > provide more input?

The subprocess in a purely passive position: it is an interpreter: I
send it commands and I read the answers. The python side is in charge.

> 2. The subprocess has blocked because it has filled its stderr buffer 
> and is waiting for something (i.e. your program) to read it.

No, that's not it or it would hang immediately. I can get a few lines
out of stdout and then I hang because I can't tell when it's time to stop
pumping. But you are right that if there was something on stderr I would
be in trouble.

Regards,

-- O.L.




More information about the Python-list mailing list