Repost: Read a running process output

Nobody nobody at nowhere.com
Fri Feb 5 23:41:22 CET 2010


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




More information about the Python-list mailing list