0

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
bigbounty
  • 16,526
  • 5
  • 37
  • 65
Nexon
  • 326
  • 1
  • 11

2 Answers2

0

I think you have to clean your console :))

see this question :

clear console in python

0

You can print backspaces, like this:

import time, sys

t = 0
while True:
    print('Seconds passed:', t, end='')
    sys.stdout.flush()
    time.sleep(1)
    t += 1
    print('\b'*20, end='')
dg-vwp
  • 88
  • 4