read and readline hanging

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Jan 28 05:05:42 EST 2008


On Sun, 27 Jan 2008 19:58:27 +0100, Olivier Lefevre wrote:

>>> But how can I find out *programmatically* that there is no more
>>> input?
>> 
>> You can't.
> 
> How do people handle this, then? Reading from a process that will
> block if you ask too much yet won't let you know how much there is
> to read right now has to be some kind of FAQ.

It's impossible to handle if the external process does not tell you
somehow if there's still data ahead or if it is finished.  Then there's
only the closing of the file on the process' side that tells you the
definitive end of the data.

>> This doesn't answer if the interpreter doesn't flush its output buffer
>> after every line.
> 
> I think it must otherwise you might get incomplete answers or no
> answers at the interactive prompt and that never happens. It may
> not flush its buffer after every line but it must flush them at
> the end of an answer.

The buffering behavior at the interactive prompt is very often different
from connections via pipes.  If stdout of a process is connected to a
terminal the standard C library chooses line buffering but if it is
connected to a pipe or redirected to a file it chooses block buffering
instead.

In such cases the `pexpect` module might be a solution.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list