Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 环路中断tqdm_Python_Tqdm - Fatal编程技术网

Python 环路中断tqdm

Python 环路中断tqdm,python,tqdm,Python,Tqdm,以下简单代码用于在循环上迭代时显示进度条: import tqdm for f in tqdm.tqdm(range(100000000)): if f > 100000000/4: break 执行中断时失败: $ python test.py 24%|████▎ | 24425076/100000000 [00:03<00:11, 6550673.18it/s] Exception KeyError: KeyError(<weakref at 0x7fb8

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

import tqdm
for f in tqdm.tqdm(range(100000000)):
  if f > 100000000/4:
    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
我在Internet上查找了类似的错误,但没有得到积极的结果。

结果表明,TQM迭代器在中断时必须手动关闭:

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

这执行起来没有任何问题。

我不使用库,但看起来你是故意以25%的价格突破?进度条应该如何响应?我试图在一个循环中使用TQM,当给定条件为真时,该循环可以停止。@roganjosh当发生错误时,您可能希望更新或放弃进度条,而不会出现其他可能会混淆以下逻辑或提供给用户的消息的错误。
import tqdm
iterator = tqdm.tqdm(range(100000000))
for f in iterator:
  if f > 100000000/4:
    iterator.close()
    break