赞
踩
import multiprocessing as mp def job(x): return x*x def multicore(): pool = mp.Pool() # 无参数时,使用所有cpu核 # pool = mp.Pool(processes=3) # 有参数时,使用CPU核数量为3 res = pool.map(job, range(10)) print(res) if __name__ == '__main__': multicore() 运行结果: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
import multiprocessing as mp import itertools def job(r, item): (x, y) = item return x * y def multicore(z): x_y = list(itertools.product(range(10), range(10))) pool = mp.Pool() # 无参数时,使用所有cpu核 # pool = mp.Pool(processes=3) # 有参数时,使用CPU核数量为3 res = pool.map(job, x_y) return res if __name__ == '__main__': res = multicore() print(res)
import multiprocessing as mp import itertools from functools import partial def job(z, r, item): (x, y) = item return x * y + z + r def multicore(z): x_y = list(itertools.product(range(10), range(10))) r = 2 func = partial(job, z, r) pool = mp.Pool() # 无参数时,使用所有cpu核 # pool = mp.Pool(processes=3) # 有参数时,使用CPU核数量为3 res = pool.map(job, x_y) return res if __name__ == '__main__': res = multicore(1) print(res)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。