Python test.py script is collecting data non-stop. To exit script - ctrl/c is expected.
File test.log is empty while script is running. Output to test.log is created only after script is finished.
It is running on windows 2008 server.
How to save ouput "on the fly", so I can check test.log and see the progress?
from time import sleep import sys class Logger(object): def __init__(self, filename="Default.log"): self.terminal = sys.stdout self.log = open(filename, "a") def write(self, message): self.terminal.write(message) self.log.write(message) def main(): signaled_to_stop = False sheep_counter = 1 sys.stdout = Logger("test.log") print "Running.. (press CTRL-C to stop)" while True: try: # Wait 3 seconds between each step. sleep(3) print "Counting sheeps... %s" % sheep_counter sheep_counter = sheep_counter + 1 # If signaled to stop then - Create a peepfile and stop the loop if signaled_to_stop: print "Stop requested..." if signaled_to_stop: break except KeyboardInterrupt: print "\nStopping (might take some seconds).." signaled_to_stop = True except SystemExit as err: raise err print "Process has finished." # If this file is executed directly then call the main() method if __name__ == "__main__": main()
Output is like:
python test.py Running.. (press CTRL-C to stop) Counting sheeps... 1 Counting sheeps... 2 Counting sheeps... 3 Stopping (might take some seconds).. Counting sheeps... 4 Stop requested... Process has finished.