Testing for the presence of input from stdin.

Thomas Bellman bellman at lysator.liu.se
Tue Jan 24 05:39:28 EST 2006


Peter Gsellmann <peter-gsellmann at eunet.at> writes:

> Will McDonald wrote:

>> That's a good point. I did wonder if it'd just have to sit there
>> waiting for input much like cat would. I think that's preferable, and
>> simpler :), than implementing timeouts.
>> 
> In unix you can always use select.select() on files and pipes as sys.stdin
> is. Use a timout-value of 0 and you get the 'ready-state' of the file
> descriptor i.e. the presence of waiting input-data.

If you terminate when select() indicates that there is nothing
more to read, you will terminate prematurely in many cases.  Even
'dd if=/dev/zero | myprogram.py' will stop at some random point,
when the OS happens to decide that myprogram.py should be scheduled
twice without dd getting the chance to fill the pipe buffer
inbetween.


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"We don't understand the software, and        ! bellman @ lysator.liu.se
 sometimes  we don't understand the hardware, ! 
 but we can *see* the blinking lights!"       ! Make Love -- Nicht Wahr!



More information about the Python-list mailing list