python tqdm 多线程_python – 使用tqdm与concurrent.futures?

您可以将tqdm包装在执行程序周围,如下所示,以跟踪进度:

list(tqdm(executor.map(f,iter),total = len(iter))

这是你的例子:

import time

import concurrent.futures

from tqdm import tqdm

def f(x):

time.sleep(0.001) # to visualize the progress

return x**2

def run(f, my_iter):

with concurrent.futures.ThreadPoolExecutor() as executor:

results = list(tqdm(executor.map(f, my_iter), total=len(my_iter)))

return results

my_iter = range(100000)

run(f, my_iter)

结果是这样的:

16%|██▏ | 15707/100000 [00:00<00:02, 31312.54it/s]


版权声明:本文为weixin_30336531原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。