from multiprocessing import Pool from tqdm import tqdm import multiprocessing import time def preprocess_data_multiprocessing(x): time.sleep(0.1) return x+5 data = range(20) if __name__ == '__main__': multiprocessing.freeze_support() t1 =time.time() with Pool(2) as p: examples = list( tqdm( p.imap(preprocess_data_multiprocessing, data), total=len(data), disable=False, ) ) print(time.time()-t1) print(examples)
#结果
100%|██████████| 20/20 [00:01<00:00, 17.35it/s]
1.5249207019805908
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]