lackadaisical
lackadaisical

Reputation: 1694

Loop break breaking 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.

Upvotes: 15

Views: 9468

Answers (1)

lackadaisical
lackadaisical

Reputation: 1694

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.

Upvotes: 25

Related Questions