Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Repost: Read a running process output

4 views
Skip to first unread message

Ashok Prabhu

unread,
Feb 5, 2010, 6:57:17 AM2/5/10
to
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.

Alain Ketterlin

unread,
Feb 5, 2010, 7:12:55 AM2/5/10
to
Ashok Prabhu <ashokp...@gmail.com> 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.

Ashok Prabhu

unread,
Feb 5, 2010, 7:53:05 AM2/5/10
to
On Feb 5, 5:12 pm, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:

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.

Alain Ketterlin

unread,
Feb 5, 2010, 7:58:27 AM2/5/10
to
Ashok Prabhu <ashokp...@gmail.com> 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.

Ashok Prabhu

unread,
Feb 5, 2010, 8:33:00 AM2/5/10
to
On Feb 5, 5:58 pm, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:

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

unread,
Feb 5, 2010, 8:39:49 AM2/5/10
to

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
...

Helmut Jarausch

unread,
Feb 5, 2010, 11:31:08 AM2/5/10
to

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

Nobody

unread,
Feb 5, 2010, 5:41:22 PM2/5/10
to

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").

0 new messages