How Can I Capture Output And Show It At The Same Time With Python?
I have a pretty long running job, which runs for several minutes and then gets restarted. The task outputs various information which I capture like this: output = subprocess.Popen(
Solution 1:
You can try something like this :
cmd = ["./my_program.sh"]
p = subprocess.Popen( cmd, shell=False, stdout=subprocess.PIPE) # launch the processwhile p.poll() isNone: # check if the process is still alive
out = p.stdout.readline() # if it is still alive, grab the output
do_something_with(out) # do what you want with it
Solution 2:
You could read it one line at a time:
from subprocess import Popen, PIPE
p = Popen('grep -ir graph .', stdout=PIPE)
whilenot p.returncode:
s = p.stdout.readline()
print s
p.poll()
In this way, you are only blocking for the time it takes to process to output a single line.
Solution 3:
You can use the "tee" command. It does exactly what you need from it. http://www.computerhope.com/unix/utee.htm
Post a Comment for "How Can I Capture Output And Show It At The Same Time With Python?"