Repost: Read a running process output

A

Ashok Prabhu

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

Alain Ketterlin

Ashok Prabhu said:
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.
 
A

Ashok Prabhu

Ashok Prabhu said:
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.
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.
 
A

Alain Ketterlin

Ashok Prabhu said:
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.

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

Ashok Prabhu

Ashok Prabhu said:
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
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.
 
A

Ashok Prabhu

Ashok Prabhu said:
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

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

Helmut Jarausch

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
 
N

Nobody

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top