Testing for the presence of input from stdin.

Diez B. Roggisch deets at nospam.web.de
Mon Jan 23 08:21:19 EST 2006


> I can get the script to behave as expected when content's piped to it
> using sys.stdin but I'd like to know that there's data coming from
> stdin or fail and print the useage again. Is there a simple way to
> achieve this?

There are more experienced UNIXers here, but from my POV I don't see how
that can happen. The reason is simply that

 - sys.stdin alwasy exists (unless you close it yourself)
 
 - in a pipe (which this essentially is) there is now way to know if there
is more date to come or not, except for the "broken pipe" error - but that
won't happen to you, as sys.stdin is not broken just because there is
currently no data arriving.


The only thing I can think of is to introduce a timeout. If no data has
arrived for a certain amount of time, you terminate. That could be achieved
using either threads, or non-blocking IO.

However, it's a heuristic. If your aunt sally starts the program and wants
to feed it manually, but is slow because she first has to fetch her glasses
from the kitchen, she'll think you're nasty because you terminated her
program...

Diez




More information about the Python-list mailing list