faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 17 people (71%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Non-blocking I/O

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 230, Fredrik Lundh


"""
Packages: basic_applications;core_python
"""
"""
> 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>
"""