將python stdout設定為不緩衝

weixin_34279184發表於2012-10-15

    # 將stdout設定為不緩衝
    sys.stdout = Unbuffered(sys.stdout)

 

class Unbuffered:
    def __init__(self, stream):
        self.stream = stream

    def write(self, data):
        self.stream.write(data)
        self.stream.flush()

    def __getattr__(self, attr):
        return getattr(self.stream, attr)

相關文章