469,955 Members | 2,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

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/
Jun 27 '08 #1
1 4147
On Jun 21, 12:29 pm, Yang Zhang <yanghates...@gmail.comwrote:
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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Seong Jin, Cho | last post: by
5 posts views Thread by Max Rabkin | last post: by
5 posts views Thread by Rich | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
1 post views Thread by Sebouh | last post: by
5 posts views Thread by Joakim Hove | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.