# cmd = "python subscript.py"
cmd = "ping localhost -n 10"
ofile =open("C:\file.log","w")
sp = subprocess.Popen(cmd,bufsize = 1, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
while True:
sp.poll()
line = sp.stdout.readline()
#eline = sp.stderr.readline()
if line:
print line
ofile.write(line)
#if eline:
# print eline
# ofile.write(" ERROR: "+line)
# if (line == "" and eline == ""):
if (line == ""):
break
I'm trying to get output from a subprocess and save it to a log file using the code above. It works well for the ping localhost -n 10
. But when using it to call subscript.py
, I can not get the output of subscript.py
in real time. I will get all output after the termination of subscript.py
. any suggestion? Also I have to comment out eline = sp.stderr.readline()
in order to get it work. Any idea why the some code won't give me real time output of subscript.py
?
subscript.py:
import time
i=0
while (i<5):
time.sleep(1)
i += 1
print "ouput:",i
As rubik mentioned there are couple similar questions asked before. I tried all I found and none of them solve my problem. Hope some one can point the reason why its not working when calling subscript.py
.
edit: problem here: the output of subscript.py was not flushed until the termination of itself. also the subscript.py does not have any stderr so calling sp.stderr.readline() causes an infinite wait. solution: flush output in subscript.py for all stderr, I just use stderr = subprocess.STDOUT to redirect to stdout.