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