Search code examples
pythontqdm

Tqdm status bar shown before executed


I have a tqdm progress bar:

print('foo')
for status in tqdm(cursor.items(count)):
    #process status
    pass

I print some messages before the loop but the progress bar is shown before them. Is there any kind of multi threading or how can i fix this?


Solution

  • tqdm works in a thread (that's good as the application won't stuck because of the progress bar) and therefore the progress bar is shown before the prints.

    As the machine sees that print is an IO action so the machine gives priority to the tqdm.

    You need to sleep just before and after the loop. To do so, use time.sleep(x)(x is in seconds) before and after the loop to stop the problem. Remember to import time at the start of your code. Experiment with different values of x, but just 0.1 will probably work fine.