Flush stdin

Cameron Simpson cs at zip.com.au
Sat Oct 18 03:11:45 EDT 2014


On 17Oct2014 12:38, Empty Account <emptya45 at gmail.com> 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, so I am using the termios
>module. 

You're aware that a stdio flush and a termios flush operate on two totally 
unrelated buffers?

>while True: 
>   input = sys.stdin.readline()
>   # do some parsing 
>   …  
>   sys.stdout.write(parsed_data)
>   time.sleep(3)
>   termios.tcflush(sys.stdin, termios.TCIOFLUSH)
>
>I am receiving this exception
>termios.error: (25, 'Inappropriate ioctl for device')

That is because stdin is attached to the pipe from netcat. A pipe is not a 
terminal.

>I will be using this script on Unix based systems and I wondered what
>approach I could use 
>to flush stdin?

Like Chris, I think you need to explain why you even want to flush stdin.  
There's probably something better you can do.

Cheers,
Cameron Simpson <cs at zip.com.au>

I strongly suspect so.  Practically everyone on sci.physics has a theory that
is far superior to special relativity, general relativity, quantum mechanics
*and* the standard model.  Around here, it's only a small clique of arrogant
young members of the physics establishment who fail to recognize these
revolutionary theories.  I'd explain why, but I have to go finish designing
my faster-than-light vacuum energy perpetual motion telekinetic
aether-powered time machine.    - John Baez



More information about the Python-list mailing list