Python cannot allocate memory using multiprocessing.pool

前端 未结 1 834
一个人的身影
一个人的身影 2020-12-16 18:26

My code (part of a genetic optimization algorithm) runs a few processes in parallel, waits for all of them to finish, reads the output, and then repeats with a different inp

相关标签:
1条回答
  • 2020-12-16 19:06

    As shown in the comments to my question, the answer came from Puciek.

    The solution was to close the pool of processes after it is finished. I thought that it would be closed automatically because the results variable is local to RunMany, and would be deleted after RunMany completed. However, python doesn't always work as expected.

    The fixed code is:

    def RunMany(inputs):
    from multiprocessing import cpu_count, Pool
    proc=inputs[0]
    pool=Pool(processes = proc) 
    results=[]
    for arg1 in inputs[1]:
        for arg2 in inputs[2]:
            for arg3 in inputs[3]:
                results.append(pool.apply_async(RunOne, args=(arg1, arg2, arg3)))
    #new section
    pool.close()
    pool.join()    
    #end new section
    casenum=0
    datadict=dict()
    for p in results:
        #get results of simulation once it has finished
        datadict[casenum]=p.get() 
        casenum+=1
    return datadict
    
    0 讨论(0)
提交回复
热议问题