0
# 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.

wang
  • 13
  • 6
  • Possible duplicate: http://stackoverflow.com/questions/803265/getting-realtime-output-using-subprocess – rubik Dec 13 '11 at 20:30
  • Other duplicate: http://stackoverflow.com/questions/1085071/real-time-intercepting-of-stdout-from-another-process-in-python. Actually there are *a lot* of duplicates, you just need to search. http://stackoverflow.com/questions/527197/intercepting-stdout-of-a-subprocess-while-it-is-running – rubik Dec 13 '11 at 20:35
  • I did search and tried all code I found on stackoverflow. The key here is that the same code tends to not work when calling a second python script. – wang Dec 13 '11 at 20:44

1 Answers1

2

Have you tried calling sys.stdout.flush() in your subprocess after the print statement? You could be running into output buffering.

Mattie
  • 20,280
  • 7
  • 36
  • 54
  • Yes I have tied that one but its a no go. For what ever reason all the output will be displayed after the termination of subscript.py – wang Dec 13 '11 at 21:46
  • Misunderstood you. Tried to call sys.stdout.flush() after print statement in subscript.py and it works. – wang Dec 14 '11 at 20:18