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:
- Does
tqdmuse a separate thread or subprocess for updating the progress bar? - How does
tqdmhandle progress bar updates when there are nested loops with print statements in them?
To answer your questions:
I don't believe
tqdmcreates 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.If you reran your code as I had suggested, you now know how
tqdmworks withprintcalls, which is to say, not very well. Try usingtqdm.writeinstead: