Line buffering pipes

S

Shaun Jackman

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.

Please cc me in your reply. Thanks,
Shaun
 
D

Dave Cole

Shaun said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top