python - Getting progress message from a subprocess -
i want start program needs several minutes complete. during time want read progress message of program (which printed on stdout). problem cannot find way read out output during run.
the function found read out output of program popen.communicate()
, method waits until process finishes. impossible progress , make visible user in special formatted way.
is possible way?
when run process subprocess.popen
script see output of program on screen. possible hide it? (ubuntu 10.10, normal terminal)
simplest call popen keyword argument stdout=subprocess.pipe
.
p = subprocess.popen(["ls"], stdout=subprocess.pipe) while true: line = p.stdout.readline() if not line: break print line
to see in action, here 2 sample scripts. make them both in same directory , run python superprint.py
printandwait.py:
import time import sys print 10 sys.stdout.flush() time.sleep(10) print 20 sys.stdout.flush()
superprint.py:
import subprocess import sys p = subprocess.popen(["python printandwait.py"], shell=true, stdout=subprocess.pipe) while true: print "looping" line = p.stdout.readline() if not line: break print line.strip() sys.stdout.flush()
Comments
Post a Comment