循环断裂打破tqdm

lac*_*cal 5 python tqdm

以下简单代码使用tqdm在迭代循环时显示进度条:

import tqdm
for f in tqdm.tqdm(range(100000000)):
  if f > 100000000/4:
    break
Run Code Online (Sandbox Code Playgroud)

执行中断时失败:

$ 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
Run Code Online (Sandbox Code Playgroud)

我使用的是Python v2.7.6和tqdm v4.32.1:

$ python --version
Python 2.7.6
$ python -m tqdm --version
4.23.1
Run Code Online (Sandbox Code Playgroud)

我在互联网上寻找类似的错误,没有任何积极的结果.

lac*_*cal 8

事实证明,tqdm迭代器在被中断时必须手动关闭:

import tqdm
iterator = tqdm.tqdm(range(100000000))
for f in iterator:
  if f > 100000000/4:
    iterator.close()
    break
Run Code Online (Sandbox Code Playgroud)

这没有问题.