为什么 tqdm 仅在多次迭代后更新?

Why is tqdm only updating after multiple iterations?

我在我的脚本中使用了两次 tqdm,第一次它工作正常,但第二次它只在 14 次迭代后更新。如果我删除所有其他打印语句也是一样的。知道可能出了什么问题吗?

节目:

for name in tqdm(final_urls):
    print(f"Downloading {name} files...")
    url_list = final_urls[name]
    class_dir = os.path.join("data", name)

    if not os.path.isdir(class_dir):
        os.mkdir(class_dir)

    for idx, url in enumerate(url_list):
        filepath = os.path.join(class_dir, f"audio{idx}.wav")
        if not os.path.isfile(filepath):
            r = requests.get(url)
            with open(filepath, "wb+") as f:
                f.write(r.content)

输出:

Download files...
  0%|                                                                                                                   | 0/32 [00:00<?, ?it/s]Downloading Bearded Seal files...
Downloading Bottlenose Dolphin files...
Downloading Rough-Toothed Dolphin files...
Downloading Common Dolphin files...
Downloading Striped Dolphin files...
Downloading Fin, Finback Whale files...
Downloading Melon Headed Whale files...
Downloading Killer Whale files...
Downloading False Killer Whale files...
Downloading Spinner Dolphin files...
Downloading Ross Seal files...
Downloading Harp Seal files...
Downloading Northern Right Whale files...
Downloading Short-Finned (Pacific) Pilot Whale files...
 44%|██████████████████████████████████████████████▍                                                           | 14/32 [01:34<02:01,  6.74s/it]Downloading White-beaked Dolphin files...
Downloading Narwhal files...
Downloading Humpback Whale files...
Downloading Walrus files...
Downloading Fraser's Dolphin files...
Downloading Beluga, White Whale files...
Downloading Clymene Dolphin files...
Downloading Southern Right Whale files...
Downloading Atlantic Spotted Dolphin files...
Downloading Leopard Seal files...
Downloading Long-Finned Pilot Whale files...
Downloading Grampus, Risso's Dolphin files...
Downloading Sperm Whale files...
Downloading Pantropical Spotted Dolphin files...
Downloading Bowhead Whale files...
Downloading Minke Whale files...
Downloading White-sided Dolphin files...
Downloading Weddell Seal files...
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 32/32 [01:34<00:00,  2.95s/it]

默认情况下,如果更新发生得很快,tqdm 不会显示每一个更新;默认情况下它每秒只更新 10 次。如果必须在每次迭代时更新输出,则可以将 miniters 参数设置为 1

默认为miniters=None,这意味着它将根据mininterval动态调整迭代次数,设置为0.1秒。

您还在使用 print(),它 替换了 栏输出。不要那样做,更新将被覆盖,你会得到非常混乱的输出。

tqdm class 有一个专用的 tqdm.write() method,请改用它:

for name in tqdm(final_urls):
    tqdm.write(f"Downloading {name} files...")

根据文档,使用该方法让 tqdm 处理消息的放置方式:

Print a message via tqdm (without overlap with bars).

使用 miniters=1:

显示差异的快速演示
>>> from tqdm import tqdm
>>> from faker import Faker
>>> import time, random
>>> for word in tqdm(Faker().words(10), ncols=40, miniters=1):
...     print(f"Using print: {word}")
...     time.sleep(random.random() * .2)
...
  0%|            | 0/10 [00:00<?, ?it/s]Using print: of
 10%|▍   | 1/10 [00:00<00:01,  5.02it/s]Using print: we
 20%|▊   | 2/10 [00:00<00:01,  5.52it/s]Using print: message
 30%|█▏  | 3/10 [00:00<00:01,  5.83it/s]Using print: win
Using print: seem
 50%|██  | 5/10 [00:00<00:00,  9.12it/s]Using print: especially
 60%|██▍ | 6/10 [00:00<00:00,  8.00it/s]Using print: power
 70%|██▊ | 7/10 [00:00<00:00,  8.42it/s]Using print: material
Using print: both
 90%|███▌| 9/10 [00:01<00:00,  8.27it/s]Using print: single
100%|███| 10/10 [00:01<00:00,  8.09it/s]
>>> for word in tqdm(Faker().words(10), ncols=40, miniters=1):
...     tqdm.write(f"Using tqdm.write: {word}")
...     time.sleep(random.random() * .2)
...
Using tqdm.write: democratic
Using tqdm.write: manage
Using tqdm.write: generation
Using tqdm.write: perhaps
Using tqdm.write: purpose
Using tqdm.write: about
Using tqdm.write: evidence
Using tqdm.write: everything
Using tqdm.write: push
Using tqdm.write: large
100%|███| 10/10 [00:01<00:00,  7.65it/s]

并且,仍在使用 tqdm.write()miniters=1 进行中: