Unbuffered stdout/auto-flush

s0suk3 at gmail.com s0suk3 at gmail.com
Sun Jun 22 02:26:39 CEST 2008

On Jun 21, 12:29 pm, Yang Zhang <yanghates... at gmail.com> wrote:
> Hi, is there any way to get unbuffered stdout/stderr without relying on
> the -u flag to python or calling .flush() on each print (including
> indirect hacks like replacing sys.stdout with a wrapper that succeeds
> each write() with a flush())?  Thanks in advance!

I think the only way is to reopen the stdout file descriptor:

import sys
import os

# reopen stdout file descriptor with write mode
# and 0 as the buffer size (unbuffered)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

print "unbuffered text"


More information about the Python-list mailing list