Created
October 30, 2013 09:12
-
-
Save zhenyi2697/7229421 to your computer and use it in GitHub Desktop.
Python: continuously read content from subprocess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import time | |
cmd = 'python ./output.py' | |
proc = subprocess.Popen( | |
cmd, | |
shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
) | |
output = '' | |
while True: | |
result = proc.poll() | |
delta = proc.stdout.read(1) | |
if result is not None: | |
print 'terminated' | |
break | |
if delta != ' ': | |
output = output + delta | |
else: | |
if '%' in output: | |
print 'percentage is:' | |
print output | |
elif '/s' in output: | |
print 'speed is:' | |
print output | |
print 'output is:' | |
print output | |
output = '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment