Last active
August 29, 2015 14:21
-
-
Save donkirkby/8f83b2d6cb60e8041b33 to your computer and use it in GitHub Desktop.
Subprocess output iteration
This file contains 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
import subprocess | |
p = subprocess.Popen(['ping', '-c', '10', 'google.com'], stdout=subprocess.PIPE) | |
print 'Started.' | |
use_iterator = False | |
if use_iterator: | |
# This is buffered, so waits for several pings. | |
for line in p.stdout: | |
print line, | |
else: | |
# Reads lines immediately. | |
for line in iter(p.stdout.readline, ''): | |
print line, | |
p.wait() | |
print 'Finished.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment