2

I want to log the real time stdout from a different Class to a log file. Here is my demo code. Code works well. But it is waiting for the sample function to finish. I want to run it in parallel. So the log file will written in same time of stdout prints.

import sys
import time

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("log.txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)  

class sample:
  def __init__(self):
    print "TEST"
    time.sleep(5)

if __name__ == "__main__":
  a = sample()
  sys.stdout = Logger()
2
  • Did you try: self.log.flush()? after the write? Note that sys.stdout buffering can change depending on a number of factors/options, so you'd better flush() that as well. Commented Jun 3, 2014 at 9:15
  • @Bakuriu ..Thanks its worked. Also I have to call sys.stdout = Logger() before a = sample() Commented Jun 3, 2014 at 9:25

1 Answer 1

5

Calls to file.write do not necessarily write the contents to the HD immediately. It depends on the buffer policy of the file object. If you want to force writing to disk at a certain moment in time, you can use the flush() method (see also this).

Note that sys.stdout flushing policy depends on the configuration of your installation and also on environmental variables, so if you want to guarantee "parallel" writes between standard output and the log file you must flush() both streams:

def write(self, message):
    self.terminal.write(message)
    self.log.write(message)  
    self.terminal.flush()
    self.log.flush()
Sign up to request clarification or add additional context in comments.

1 Comment

the os.fsync() in your reference link helps to solve my issue!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.