Line buffering pipes
djc at object-craft.com.au
Wed Nov 3 02:20:40 CET 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')
> while True: print tcpdump.next(),
> 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.
More information about the Python-list