为什么tqdm经过多次迭代才更新?

Shr*_*shi 4 python printing tqdm

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

\n

程序:

\n
for name in tqdm(final_urls):\n    print(f"Downloading {name} files...")\n    url_list = final_urls[name]\n    class_dir = os.path.join("data", name)\n\n    if not os.path.isdir(class_dir):\n        os.mkdir(class_dir)\n\n    for idx, url in enumerate(url_list):\n        filepath = os.path.join(class_dir, f"audio{idx}.wav")\n        if not os.path.isfile(filepath):\n            r = requests.get(url)\n            with open(filepath, "wb+") as f:\n                f.write(r.content)\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

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

Mar*_*ers 11

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

\n

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

\n

您还使用print(),它取代了条形输出。不要这样做,更新将被覆盖,并且您会得到非常混乱的输出。

\n

该类tqdm有一个专用tqdm.write()方法,请使用它:

\n
for name in tqdm(final_urls):\n    tqdm.write(f"Downloading {name} files...")\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
\n

通过 tqdm 打印消息(不与条重叠)。

\n
\n

快速演示以显示差异,同时使用miniters=1

\n
>>> from tqdm import tqdm\n>>> from faker import Faker\n>>> import time, random\n>>> for word in tqdm(Faker().words(10), ncols=40, miniters=1):\n...     print(f"Using print: {word}")\n...     time.sleep(random.random() * .2)\n...\n  0%|            | 0/10 [00:00<?, ?it/s]Using print: of\n 10%|\xe2\x96\x8d   | 1/10 [00:00<00:01,  5.02it/s]Using print: we\n 20%|\xe2\x96\x8a   | 2/10 [00:00<00:01,  5.52it/s]Using print: message\n 30%|\xe2\x96\x88\xe2\x96\x8f  | 3/10 [00:00<00:01,  5.83it/s]Using print: win\nUsing print: seem\n 50%|\xe2\x96\x88\xe2\x96\x88  | 5/10 [00:00<00:00,  9.12it/s]Using print: especially\n 60%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x8d | 6/10 [00:00<00:00,  8.00it/s]Using print: power\n 70%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x8a | 7/10 [00:00<00:00,  8.42it/s]Using print: material\nUsing print: both\n 90%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x8c| 9/10 [00:01<00:00,  8.27it/s]Using print: single\n100%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 10/10 [00:01<00:00,  8.09it/s]\n>>> for word in tqdm(Faker().words(10), ncols=40, miniters=1):\n...     tqdm.write(f"Using tqdm.write: {word}")\n...     time.sleep(random.random() * .2)\n...\nUsing tqdm.write: democratic\nUsing tqdm.write: manage\nUsing tqdm.write: generation\nUsing tqdm.write: perhaps\nUsing tqdm.write: purpose\nUsing tqdm.write: about\nUsing tqdm.write: evidence\nUsing tqdm.write: everything\nUsing tqdm.write: push\nUsing tqdm.write: large\n100%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88| 10/10 [00:01<00:00,  7.65it/s]\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

终端屏幕的动画 GIF 图像,进度条从 0% 进展到 100%

\n