Sunday, September 19, 2010

Flusing output in Python

When you use "print" statement to output messages to terminal during the execution of a python program, those messages might not get outputted promptly. This is true for time consuming programs that perform a lot of work in between print statements. To promptly flush the text to terminal invoke the python interpreter with "-u" option. For example, if your program is myprogram.py then do the following.

python -u myprogram.py
Alternatively, if you are using sys.stdout.write to print messages to the terminal, then you can do sys.stdout.flush() right after the sys.stdout.write() to do the same. This works even with file I/O.
file.flush() immediately writes the text that is in buffer. This is particularly useful when you are writing log files.

No comments:

Post a Comment

Continuously monitor GPU usage

 For nvidia GPUs do the follwing: nvidia-smi -l 1