read pipe information on a running (popen) process

Discussion in 'Python' started by Jules Stevenson, Jan 6, 2009.

  1. Hi,

    I'm really struggling with how to read the stdout on a running process. All
    the examples I've seen tend to rely on waiting for the process to finish.
    The process I'm running takes a while and I need to get stdout [and stderr]
    and be able to pipe them into a string for displaying in a web app.

    My shoddy code is currently this:

    p = subprocess.Popen('xsibatch.exe -s', bufsize=200, shell=True,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    while p.returncode==None:
    p.poll()
    print p.stdout.readline(), p.stderr.readline(),
    time.sleep(1)

    print 'finished'

    Which results in it printing (i think) the last line written to the pipe
    file. I'm thinking there must be a simpler way to access this information.
    Ideally I'd like to dump the whole lot to a string at periodic intervals but
    am completely stumped as to how to do this as readlines() and many other
    methods require an EOF.

    On a side note [and much lesser problem currently], I was under the
    impression that p.poll() writes the returncode attribute, but from what I
    can see when the process terminates p.poll() still returns none?

    Many thanks,

    Jules
    Jules Stevenson, Jan 6, 2009
    #1
    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. P.S.
    Replies:
    0
    Views:
    316
  2. Miki Tebeka
    Replies:
    2
    Views:
    593
    Chris Angelico
    Apr 8, 2011
  3. Daniel Berger
    Replies:
    5
    Views:
    681
    Nobuyoshi Nakada
    Feb 21, 2008
  4. Pito Salas
    Replies:
    11
    Views:
    239
    Robert Klemme
    Aug 26, 2009
  5. Richard
    Replies:
    2
    Views:
    133
    Brian McCauley
    May 27, 2005
Loading...

Share This Page