I am writing a program with multiple threads, thus for logging information I use the python logging module. However I have parts of application where I would like to display how much is a thread is waiting for execution in the same line which is overwritten each time. I do not want multiple lines to show in the log.
I tried to use the sys.stdout.flush()
function. Unfortunately it doesn't work.
My current code:
def log_flush(message, start, stop):
import logging, sys
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
time = stop - start
logging.debug((str(message) + str(time)))
sys.stdout.write('\r')
sys.stdout.flush()
for i in range(0, 5):
log_flush("Time taken: ", 0, i)
My expected output is (after execution is finished):
(MainThread) Time taken: 4
While currently the script outputs:
(MainThread) Time taken: 0
(MainThread) Time taken: 1
(MainThread) Time taken: 2
(MainThread) Time taken: 3
(MainThread) Time taken: 4