!! Non blocking IO !!

Fredrik Lundh fredrik at pythonware.com
Thu Sep 30 07:43:31 EDT 1999


Pierre Saloni wrote:
> In fact what I want is an autoflush when I print, i do not want print to
> bufferize at all.

the easiest way to do this is to use the -u option
to the interpreter.  this also enables binary stdio
where necessary:

$ python -?
Unknown option: -?
usage: python [option] ... [-c cmd | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
/.../
-u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
/.../

...

an alternative solution is to use an autoflushing
wrapper:

class AutoFlush:
    def __init__(self, stream):
        self.stream = stream
    def write(self, text):
        self.stream.write(text)
        self.stream.flush()

import sys

sys.stdout = AutoFlush(sys.stdout)
sys.stderr = AutoFlush(sys.stderr)

print "hello"

...

(maybe the CGI module should contain a
helper function that does exactly this?)

</F>





More information about the Python-list mailing list