Repost: Read a running process output

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

  1. Ashok Prabhu

    Ashok Prabhu Guest

    Hi,

    I very badly need this to work. I have been googling out for a week
    with no significant solution. I open a process p1 which does keeps
    running for 4+ hours. It gives some output in stdout now and then. I
    open this process with subprocess.Popen and redirect the stdout to
    PIPE. However when I read the output with readline it blocks waiting
    forever. I need to read from p1.stdout till what it has in the PIPE.
    Can someone help me out with the exact code change that can accomplish
    the task.

    from subprocess import *

    p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

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

    Thanks in advance,
    ~Ashok.
     
    Ashok Prabhu, Feb 5, 2010
    #1
    1. Advertising

  2. Ashok Prabhu <> writes:

    > from subprocess import *
    > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)


    Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.

    -- Alain.
     
    Alain Ketterlin, Feb 5, 2010
    #2
    1. Advertising

  3. Ashok Prabhu

    Ashok Prabhu Guest

    On Feb 5, 5:12 pm, Alain Ketterlin <-strasbg.fr>
    wrote:
    > Ashok Prabhu <> writes:
    > > from subprocess import *
    > > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

    >
    > Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.
    >
    > -- Alain.


    Hi Alain,

    Thanks for the response. However it throws an error. Please find
    below.

    >>> from subprocess import *
    >>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib/python2.4/subprocess.py", line 543, in __init__
    errread, errwrite)
    File "/usr/lib/python2.4/subprocess.py", line 975, in _execute_child
    raise child_exception
    OSError: [Errno 2] No such file or directory

    Thanks,
    ~Ashok.
     
    Ashok Prabhu, Feb 5, 2010
    #3
  4. Ashok Prabhu <> writes:

    >> > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

    >>
    >> Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.
    >>
    >> -- Alain.


    > Thanks for the response. However it throws an error. Please find
    > below.
    >
    >>>> from subprocess import *
    >>>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE)


    You forgot to change the monolithic command into a list of words. Since
    you don't use the shell anymore you have to give Popen a pre-parsed
    command line.

    -- Alain.
     
    Alain Ketterlin, Feb 5, 2010
    #4
  5. Ashok Prabhu

    Ashok Prabhu Guest

    On Feb 5, 5:58 pm, Alain Ketterlin <-strasbg.fr>
    wrote:
    > Ashok Prabhu <> writes:
    > >> > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

    >
    > >> Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.

    >
    > >> -- Alain.

    > > Thanks for the response. However it throws an error. Please find
    > > below.

    >
    > >>>> from subprocess import *
    > >>>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE)

    >
    > You forgot to change the monolithic command into a list of words. Since
    > you don't use the shell anymore you have to give Popen a pre-parsed
    > command line.
    >
    > -- Alain.


    Here is the error again

    >>> p1=Popen('/usr/sunvts/bin/64/vtsk','-d',stdout=PIPE)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib/python2.4/subprocess.py", line 494, in __init__
    raise TypeError("bufsize must be an integer")
    TypeError: bufsize must be an integer


    ~Ashok.
     
    Ashok Prabhu, Feb 5, 2010
    #5
  6. Ashok Prabhu

    Ashok Prabhu Guest

    On Feb 5, 6:33 pm, Ashok Prabhu <> wrote:
    > On Feb 5, 5:58 pm, Alain Ketterlin <-strasbg.fr>
    > wrote:
    >
    >
    >
    > > Ashok Prabhu <> writes:
    > > >> > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

    >
    > > >> Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.

    >
    > > >> -- Alain.
    > > > Thanks for the response. However it throws an error. Please find
    > > > below.

    >
    > > >>>> from subprocess import *
    > > >>>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE)

    >
    > > You forgot to change the monolithic command into a list of words. Since
    > > you don't use the shell anymore you have to give Popen a pre-parsed
    > > command line.

    >
    > > -- Alain.

    >
    > Here is the error again
    >
    > >>> p1=Popen('/usr/sunvts/bin/64/vtsk','-d',stdout=PIPE)

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in ?
    >   File "/usr/lib/python2.4/subprocess.py", line 494, in __init__
    >     raise TypeError("bufsize must be an integer")
    > TypeError: bufsize must be an integer
    >
    > ~Ashok.


    Oops i missed the braces. But still no output.


    >>> p1=Popen(['/usr/sunvts/bin/64/vtsk','-d'],stdout=PIPE)
    >>> while 1:

    .... a=p1.stdout.readline()
    .... print a
    ....
     
    Ashok Prabhu, Feb 5, 2010
    #6
  7. On 02/05/10 14:39, Ashok Prabhu wrote:
    > On Feb 5, 6:33 pm, Ashok Prabhu <> wrote:
    >> On Feb 5, 5:58 pm, Alain Ketterlin <-strasbg.fr>
    >> wrote:
    >>
    >>
    >>
    >>> Ashok Prabhu <> writes:
    >>>>>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)

    >>
    >>>>> Use Popen(['/usr/...','-d'],stdout=PIPE), i.e., no shell.

    >>
    >>>>> -- Alain.
    >>>> Thanks for the response. However it throws an error. Please find
    >>>> below.

    >>
    >>>>>>> from subprocess import *
    >>>>>>> p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE)

    >>
    >>> You forgot to change the monolithic command into a list of words. Since
    >>> you don't use the shell anymore you have to give Popen a pre-parsed
    >>> command line.

    >>
    >>> -- Alain.

    >>
    >> Here is the error again
    >>
    >>>>> p1=Popen('/usr/sunvts/bin/64/vtsk','-d',stdout=PIPE)

    >>
    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in ?
    >> File "/usr/lib/python2.4/subprocess.py", line 494, in __init__
    >> raise TypeError("bufsize must be an integer")
    >> TypeError: bufsize must be an integer
    >>
    >> ~Ashok.

    >
    > Oops i missed the braces. But still no output.
    >
    >
    >>>> p1=Popen(['/usr/sunvts/bin/64/vtsk','-d'],stdout=PIPE)
    >>>> while 1:

    > ... a=p1.stdout.readline()
    > ... print a
    > ...


    I've tried

    #!/usr/bin/python
    import subprocess
    p1= subprocess.Popen(['/bin/ls','/LOCAL/'],stdout=subprocess.PIPE)
    for line in p1.stdout :
    print ">>>",line

    which works just fine.

    Are you sure, your /usr/sunvts/bin/64/vtsk writes a newline character (readline is waiting for that)?

    Helmut.

    --
    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
     
    Helmut Jarausch, Feb 5, 2010
    #7
  8. Ashok Prabhu

    Nobody Guest

    On Fri, 05 Feb 2010 03:57:17 -0800, Ashok Prabhu wrote:

    > I very badly need this to work. I have been googling out for a week
    > with no significant solution. I open a process p1 which does keeps
    > running for 4+ hours. It gives some output in stdout now and then. I
    > open this process with subprocess.Popen and redirect the stdout to
    > PIPE. However when I read the output with readline it blocks waiting
    > forever. I need to read from p1.stdout till what it has in the PIPE.
    > Can someone help me out with the exact code change that can accomplish
    > the task.
    >
    > from subprocess import *
    >
    > p1=Popen('/usr/sunvts/bin/64/vtsk -d',stdout=PIPE,shell=True)
    >
    > while 1:
    > line=p1.stdout.readline()
    > print line


    The answer is essentially the same one I gave in response to your post
    entitled "read a process output with subprocess.Popen" yesterday.

    You need to persuade the command to line-buffer its output rather than
    block-buffering it. If the command insists on block-buffering, you're out
    of luck; there's no way that Python can force it to do otherwise.

    You might be able to persuade the command to use line-buffering by using a
    pty rather than a pipe for its stdout, although that depends upon
    os.openpty() being available on your platform (the documentation only says
    "Availability: some flavors of Unix").
     
    Nobody, Feb 5, 2010
    #8
    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. rtm
    Replies:
    0
    Views:
    813
  2. Jona
    Replies:
    2
    Views:
    838
  3. John English
    Replies:
    0
    Views:
    5,284
    John English
    Nov 7, 2005
  4. Ned Balzer
    Replies:
    0
    Views:
    313
    Ned Balzer
    Dec 13, 2006
  5. Victor Hooi
    Replies:
    1
    Views:
    158
    Nobody
    Feb 10, 2013
Loading...

Share This Page