首页 > 解决方案 > python tqdm 多个进度条

问题描述

我正在尝试编写一个使用多个线程来完成其工作的程序,所以我想通过多个进度条显示进度,每个线程一个。我的代码看起来很像这样

import threading

def my_function(position):
    for i in tqdm(iterable=range(0,999999), position=position):
        pass

t1 = threading.Thread(target=my_function, args=(0,))
t2 = threading.Thread(target=my_function, args=(1,))

t1.start()
t2.start()

为此,我将在多行中更新进度条,如下所示

  0%|                                                                                                                                       | 0/999999 [00:00<?, ?it/s]
  6%|███████▋                                                                                                               | 64696/999999 [00:00<00:01, 645280.61it/s]
 12%|█████████████▉                                                                                                        | 117770/999999 [00:00<00:01, 605450.31it/s]
 16%|██████████████████▌                                                                                                   | 157511/999999 [00:00<00:01, 522733.58it/s]
 19%|██████████████████████▊                                                                                               | 193774/999999 [00:00<00:01, 461171.82it/s]
 24%|████████████████████████████                                                                                          | 237575/999999 [00:00<00:01, 453606.38it/s]
 30%|███████████████████████████████████▌                                                                                  | 300960/999999 [00:00<00:01, 495571.96it/s]
 35%|████████████████████████████████████████▊                                                                             | 345557/999999 [00:00<00:01, 479148.42it/s]
 40%|███████████████████████████████████████████████▋                                                                      | 404579/999999 [00:00<00:01, 507452.54it/s]
 45%|█████████████████████████████████████████████████████▌                                                                | 453429/999999 [00:00<00:01, 449045.09it/s]
 50%|██████████████████████████████████████████████████████████▊                                                           | 498244/999999 [00:01<00:01, 393065.80it/s]
 54%|███████████████████████████████████████████████████████████████▌                                                      | 538749/999999 [00:01<00:01, 396201.48it/s]
 58%|████████████████████████████████████████████████████████████████████▎                                                 | 579214/999999 [00:01<00:01, 398383.58it/s]
 62%|█████████████████████████████████████████████████████████████████████████                                             | 619647/999999 [00:01<00:00, 382102.28it/s]
 66%|█████████████████████████████████████████████████████████████████████████████▋                                        | 658415/999999 [00:01<00:00, 383456.33it/s]
 71%|███████████████████████████████████████████████████████████████████████████████████▎                                  | 706298/999999 [00:01<00:00, 407520.86it/s]
 75%|████████████████████████████████████████████████████████████████████████████████████████▏                             | 747737/999999 [00:01<00:00, 368022.06it/s]
 79%|████████████████████████████████████████████████████████████████████████████████████████████▋                         | 785749/999999 [00:01<00:00, 354975.84it/s]
 83%|██████████████████████████████████████████████████████████████████████████████████████████████████▎                   | 833386/999999 [00:01<00:00, 384078.11it/s]
 87%|███████████████████████████████████████████████████████████████████████████████████████████████████████               | 873912/999999 [00:02<00:00, 389926.98it/s]
 95%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████▋      | 946265/999999 [00:02<00:00, 452289.51it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 999999/999999 [00:02<00:00, 442573.76it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 999999/999999 [00:02<00:00, 443857.74it/s]

我在带有最新版本 tqdm 的 python 3.7.3 的 Windows 上。我是 python 编码的新手,所以任何帮助表示赞赏并提前感谢。

标签: pythonmultithreadingtqdm

解决方案


我自己找到了解决方案,我正在浏览 tqdm( link ) 的文档,发现 Windows 机器需要colorama它才能正常工作以用于嵌套/多条。

有一次,我安装了它,程序开始正常工作。


推荐阅读