Understanding Threading Behavior in tqdm for Progress Bar Updates

62 Views Asked by At

I am using the tqdm library in Python for progress bar updates in a nested loop structure. I noticed an unusual behavior in the progress bar updates and I'm trying to understand how tqdm manages threading (or subprocesses) for these updates. My concern is about whether tqdm uses separate threads for progress bar updates and how it interacts with standard print statements in nested loops.

Here is a simplified version of my code:

from tqdm import trange

def main():
    X = 100
    for i in trange(X):
        print(i)
        for j in range(X):
            print(j)
            k = j * i
    print("Done️")

    return 0

if __name__ == '__main__':
    main()

When I run this code, I observe that the progress bar update from tqdm is not printed until the end of the execution. Here is a snippet of the output I am getting:

[Output Snippet]
...
Done️
100%|██████████| 100/100 [00:00<00:00, 3561.95it/s]

My questions are:

  1. Does tqdm use a separate thread or subprocess for updating the progress bar?
  2. How does tqdm handle progress bar updates when there are nested loops with print statements in them?
1

There are 1 best solutions below

1
Booboo On

To answer your questions:

  1. I don't believe tqdm creates any threads to update the progress bar. And if it did, why would it matter? But your assertion that the progress bar only gets printed out at the end is not correct; it is being written out 100 times, each time on a new line. It's just scrolling by too quickly perhaps for you to notice. That is why I suggested you try my code changes and see for yourself.

  2. If you reran your code as I had suggested, you now know how tqdm works with print calls, which is to say, not very well. Try using tqdm.write instead:

from tqdm import tqdm, trange
import time

def main():
    for i in trange(100, position=0):
        tqdm.write(str(i))
        time.sleep(.1)
    print("Done️")

if __name__ == '__main__':
    main()