Proper way to use multiprocessor.Pool in a nested loop

后端 未结 3 1928
礼貌的吻别
礼貌的吻别 2020-12-05 10:40

I am using the multiprocessor.Pool() module to speed up an \"embarrassingly parallel\" loop. I actually have a nested loop, and am using multiprocessor.Pool to speed up the

3条回答
  •  死守一世寂寞
    2020-12-05 11:14

    import itertools
    import multiprocessing as mp
    
    def job(params):
        a = params[0]
        b = params[1]
        return a*b
    
    def multicore():
        a = range(1000)
        b = range(2000)
        paramlist = list(itertools.product(a,b))
        print(paramlist[0])
        pool = mp.Pool(processes = 4)
        res=pool.map(job, paramlist)
        for i in res:
            print(i)
    
    if __name__=='__main__':
        multicore()
    

    how about this?

提交回复
热议问题