Flush stdin

Marko Rauhamaa marko at pacujo.net
Mon Oct 20 06:45:22 CEST 2014


Cameron Simpson <cs at zip.com.au>:

> Even if nc itself does no buffering (handing data to the OS as soon as
> received, highly desirable for a tool like nc), the OS keeps a buffer
> for the pipeline between nc and python,

Yes, there is a buffer associated with the pipe, but linux/unix never
withholds any data from the reader. As soon as there is a single byte in
the pipe buffer, the reader process becomes ready to run and read(2) on
the pipe returns immediately.

> and python itself keeps a buffer for sys.stdin.

I found this comment in CPython's source code (pythonrun.c):

    /* stdin is always opened in buffered mode, first because it shouldn't
       make a difference in common use cases, second because TextIOWrapper
       depends on the presence of a read1() method which only exists on
       buffered streams.
    */

The solution is to use os.read().


Marko



More information about the Python-list mailing list