Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Last active August 29, 2015 14:21
Show Gist options
  • Save donkirkby/8f83b2d6cb60e8041b33 to your computer and use it in GitHub Desktop.
Save donkirkby/8f83b2d6cb60e8041b33 to your computer and use it in GitHub Desktop.
Subprocess output iteration
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