help!! getting output from another process line by line, using popen

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Jan 17 05:31:29 EST 2003


On Fri, Jan 17, 2003 at 06:06:01PM +0800, cout wrote:
> Help!!
> 
> There are 2 python scripts, one is to print out the output(output.py),
> the other is supposed to get std output from output.py line by line.
> However, it only get all the result after 40 seconds. Not during the
> executing process. Any ideas?
> 
> ===========output.py==============
> #! /usr/bin/python
> 
> import time
> 
> for i in range(1,20,1):
>         print i
>         time.sleep(2)

import sys, and do a sys.stdout.flush() after the print.

> ================monitor.py===============
> #! /usr/bin/python
> 
> import os
> import sys, string
> cmd="python output.py"

Alternatively, run "python -u output.py".

> handle = os.popen(cmd, 'r', 1)
> for line in handle:
>     print line,
> handle.close()

-Andrew.






More information about the Python-list mailing list