Tags

whoa python iterators buffer

1:01pm on Jun 21, 2013

So I've been using this code in a few programs at work:

1
2
3
4
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:

1
2
3
4
5
6
7
p = subprocess.Popen(...)
while True:
    line = p.stdout.readline()
    if not line:
        break
    ---
    print(line)

Et violà! No buffering.

Tags