Line buffering pipes

Dave Cole djc at object-craft.com.au
Tue Nov 2 20:20:40 EST 2004


Shaun Jackman wrote:
> I'm reading from a tcpdump pipe. I want to receive one line from the
> pipe as soon as its available from tcpdump. Something is buffering the
> pipe though, so I am receiving nothing, then multiple lines in a big
> batch. I thought this was the inherent buffering of next(), but it
> affects readline too.
> 
> For example,
>     tcpdump = os.popen( 'sudo tcpdump', 'r')
> both
>     while True: print tcpdump.next(),
> and
>     while True: print tcpdump.readline(),
> buffer mutiple lines before printing.

My understanding is that the libc library linked to tcpdump queries the 
file it has as standard output and discovers a pipe.  It then decides to 
buffer output for efficiency.  So what you need to do is make sure that 
the tcpdump standard output is not a pipe.

Check out the pty module.  It will connect a psuedo tty as the tcpdump 
standard output.  This will prevent the libc from buffering.

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list