whoa python iterators buffer
June 21, 2013 @ 13:01
So I’ve been using this code in a few programs at work:
p = subprocess.Popen(...)
for line in p.stdout.readline():
...
print(line)
It turns out there’s a bunch of output buffering going on here. You could put a sys.stdout.flush() after that print, but it won’t help.
The iterator buffers. Do this:
p = subprocess.Popen(...)
while True:
line = p.stdout.readline()
if not line:
break
---
print(line)
Et violà! No buffering.