python - 循环中断 breaking tqdm

标签 python tqdm

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

import tqdm
for f in tqdm.tqdm(range(100000000)):
  if f > 100000000/4:
    break

执行break时失败:

$ 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

我正在使用 Python v2.7.6 和 tq​​dm v4.32.1:

$ python --version
Python 2.7.6
$ python -m tqdm --version
4.23.1

我在 Internet 上查找过类似的错误,但没有得到积极的结果。

最佳答案

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

import tqdm
iterator = tqdm.tqdm(range(100000000))
for f in iterator:
  if f > 100000000/4:
    iterator.close()
    break

这执行没有问题。

关于python - 循环中断 breaking tqdm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017481/

相关文章:

python - 打印十六进制字符串

python - 在 tensorboard 中创建日志目录

python - 将数据透视表转换为 Pandas 中的 "tidy"数据框

python - 复厄米矩阵 : different phase angles for EIG and EIGH 的特征分析

python - 我可以向 tqdm 进度条添加消息吗?

python - tqdm 不可用时的简单回退进度条

jupyter-notebook - 无法显示类型为 HBox 的 Jupyter Widget;小部件 JavaScript 库丢失?

python3 - 打印我刚刚写入的文件

python - 如何在多处理中结合 TimeoutError 和 tq​​dm 进度条?

python - 如何在多线程中使用 tqdm?