Mailing List Archive

Unbuffered stdout/auto-flush
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!
--
Yang Zhang
http://www.mit.edu/~y_z/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Unbuffered stdout/auto-flush [ In reply to ]
On Jun 21, 12:29 pm, Yang Zhang <yanghates...@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"

Sebastian
--
http://mail.python.org/mailman/listinfo/python-list