Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop break breaking tqdm

Tags:

python

tqdm

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.

like image 484
lackadaisical Avatar asked Aug 25 '18 12:08

lackadaisical


People also ask

Does tqdm work with while loops?

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!

Is tqdm only for loops?

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.

Does tqdm affect performance?

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.

Can you use tqdm with multiprocessing?

tqdm(range(0, 30)) does not work with multiprocessing (as formulated in the code below).


Video Answer


1 Answers

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.

like image 93
lackadaisical Avatar answered Oct 24 '22 19:10

lackadaisical