Skip to content

Instantly share code, notes, and snippets.

@rtomaszewski
Created July 29, 2012 19:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtomaszewski/3201382 to your computer and use it in GitHub Desktop.
Save rtomaszewski/3201382 to your computer and use it in GitHub Desktop.
Primitive logging functions in your python scripts that are buffered before will be printed on the stdout
# Try to execute the script in two different modes to see the difference
#
# $ python log.py | tee log
# <noting for a long time>
#
# $ python -u log.py | tee log
# my log message 0
#
import time
DEBUG = 0
def log(message):
print message
def debug(message):
if DEBUG>0:
log("debug[%2d]: " % DEBUG + message)
if __name__ == '__main__':
for i in range(0,1000):
log("my log message " + str(i))
time.sleep(1)
debug("my debug message" + str(i))
@rtomaszewski
Copy link
Author

Written for this blog entry:

My python script buffers the output and it causes delays before the text appiers on the console
http://rtomaszewski.blogspot.co.uk/2012/07/my-python-script-buffers-output-and-it.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment