Line buffering pipes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shaun Jackman

    #1

    Line buffering pipes

    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.readlin e(),
    buffer mutiple lines before printing.

    Please cc me in your reply. Thanks,
    Shaun
  • Dave Cole

    #2
    Re: Line buffering pipes

    Shaun Jackman wrote:[color=blue]
    > 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.readlin e(),
    > buffer mutiple lines before printing.[/color]

    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

    Comment

    Working...