read a process output with subprocess.Popen

Discussion in 'Python' started by Ashok Prabhu, Feb 4, 2010.

  1. Ashok Prabhu

    Ashok Prabhu Guest

    Hi,

    I m trying a read the output of a process which is running
    continuously with subprocess.Popen. However the readline() method
    hangs for the process to finish. Please let me know if the following
    code can be made to work with subprocess.Popen with threads or queues.
    I tried a lot of methods but to no avail. It would be great if someone
    can make it work.

    import subprocess

    p1 = subprocess.Popen('tail -f /var/log/
    messages',stdout=subprocess.PIPE,shell=True)
    p2 = subprocess.Popen('grep
    something',stdin=p1.stdout,stdout=subprocess.PIPE,shell=True)

    while 1:
    line = p2.stdout.readline()
    print line

    Thanks,
    ~Ashok.
     
    Ashok Prabhu, Feb 4, 2010
    #1
    1. Advertising

  2. Ashok Prabhu

    Nobody Guest

    On Thu, 04 Feb 2010 04:28:20 -0800, Ashok Prabhu wrote:

    > I m trying a read the output of a process which is running
    > continuously with subprocess.Popen. However the readline() method
    > hangs for the process to finish. Please let me know if the following
    > code can be made to work with subprocess.Popen with threads or queues.
    > I tried a lot of methods but to no avail. It would be great if someone
    > can make it work.


    This is an issue with grep, not Python per se.

    By default, stdout (in the C library) is line-buffered if it refers to a
    TTY, and block-buffered otherwise (e.g. if it refers to a pipe). grep
    doesn't change the default buffering, so when it's stdout is a pipe, its
    output is written in 4K blocks.

    If you only need this to work with GNU grep, you can use the
    --line-buffered switch to force stdout to be flushed after each line.

    If it needs to be portable, you can implement the grep part in Python
    (i.e. read p1.stdout and ignore any lines which don't contain a given
    string or which don't match a given regex).
     
    Nobody, Feb 4, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Eiland
    Replies:
    3
    Views:
    550
    Peter Hansen
    May 16, 2005
  2. Trent Mick
    Replies:
    0
    Views:
    499
    Trent Mick
    May 16, 2005
  3. Replies:
    13
    Views:
    811
    Donn Cave
    Mar 6, 2007
  4. Replies:
    2
    Views:
    691
    Matt Nordhoff
    Jul 10, 2008
  5. Replies:
    2
    Views:
    467
    Gabriel Genellina
    Sep 9, 2008
Loading...

Share This Page