Flush stdin

Terry Reedy tjreedy at udel.edu
Sun Oct 19 00:35:17 CEST 2014

On 10/18/2014 5:01 PM, Cameron Simpson wrote:
> On 18Oct2014 17:55, Nobody <nobody at nowhere.invalid> wrote:
>> On Fri, 17 Oct 2014 12:38:54 +0100, Empty Account wrote:
>>> I am using netcat to listen to a port and python to read stdin and print
>>> to the console.
>>> nc -l 2003 | python print_metrics.py
>>> sys.stdin.flush() doesn’t seem to flush stdin,
>> You can't "flush" an input stream.

> Sure you can.

You are collectively confusing three different meaning of 'flush'. 
Python and its docs are the best authority on what python can and cannot 
do.  One can "call .flush() on an imput stream" (meaning 1).

 >>> import sys
 >>> sys.stdin.flush()

However, one cannot "empty the steam buffer by calling .flush()" 
(meaning 2).

" class IOBase
     Flush the write buffers of the stream if applicable. This does 
nothing for read-only and non-blocking streams."

> Most streams are read through an API which buffers. That
> buffer can be discarded.

But one can "empty and discard the buffer" (meaning 3) with

And, of course, an 'input stream' 'down here' is an 'output stream' 'up 
there, where ever' and one can 'flush' pending output into the stream so 
that it can be read here.

Terry Jan Reedy

More information about the Python-list mailing list