By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

Unbuffered stdout/auto-flush

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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.