1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| from multiprocessing import Pool import time from tqdm import tqdm
n_threads = 5
params_list = [[], []]
def Work(i): time.spleep(2) return i
def Multi_process(function, params_list, n_threads, description=""): pbar = tqdm(total=len(params_list)) pbar.set_description(description) update = lambda *args: pbar.update()
res_list = [] pool = Pool(n_threads) for param in params_list: res = pool.apply_async(func=function, args=param, callback=update) res_list.append(res) pool.close() pool.join()
out_list = [] for res in res_list: out_list.append(res.get())
return out_list
|