The following simple code uses tqdm to display a progress bar while iterating over a loop:
import tqdm
for f in tqdm.tqdm(range(100000000)):
if f > 100000000/4:
break
It fails when the break is executed:
$ python test.py
24%|████▎ | 24425076/100000000 [00:03<00:11, 6550673.18it/s]
Exception KeyError: KeyError(<weakref at 0x7fb8799f1158; to 'tqdm' at 0x7fb8799de190>,) in ignored
I am using Python v2.7.6, and tqdm v4.32.1:
$ python --version
Python 2.7.6
$ python -m tqdm --version
4.23.1
I looked for similar errors on the Internet with no positive outcome.
tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!
I think tqdm is meant for long loops, not short loops that takes a lot of time. That is because tqdm estimates the ETA based on the average time it took a cycle to complete, so it wont be that useful.
In addition, a huge benefit of using tqdm instead of a different method for showing a progress bar is that tqdm has little overhead, around 60 nanoseconds per iteration — meaning it should not affect performance much, compared to something like ProgressBar, which has an overhead of 800 nanoseconds per iteration.
tqdm(range(0, 30)) does not work with multiprocessing (as formulated in the code below).
It turns out the tqdm iterator has to be closed manually when it is interrupted:
import tqdm
iterator = tqdm.tqdm(range(100000000))
for f in iterator:
if f > 100000000/4:
iterator.close()
break
This executes without problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With