组合输出多处理python

2024-05-19 02:12:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题与previous question I asked有关,看起来很简单,但我很难找到有关多处理主题的有用信息或教程。

我的问题是,我想把产生的数据组合成一个大数组,然后将其存储在我的hdf文件中。

def Simulation(i, output):
    # make a simulation which outputs it resutlts in A. with shape 4000,3
    A = np.array([4000,3])

    output.put(A)

def handle_output(output):
    hdf = pt.openFile('simulation.h5',mode='w')
    hdf.createGroup('/','data')

    # Here the output should be joined somehow. 
    # I would like to get it in the shape [4000,3,10]

    output.get()
    hdf.createArray('/data','array',A)
    hdf.close()

if __name__ == '__main__':
    output = mp.Queue()    
    jobs = []
    proc = mp.Process(target=handle_output, args=(output, ))
    proc.start()
    for i in range(10):
        p = mp.Process(target=Simulation, args=(i, output))
        jobs.append(p)       
        p.start()
    for p in jobs:
        p.join()
    output.put(None)
    proc.join()

Tags: inoutputdataputdefjobsitmp
1条回答
网友
1楼 · 发布于 2024-05-19 02:12:08

你真正需要的是一个多处理池

做点什么,比如:

def Simulation(i): 
    return output

p = mp.Pool(16)

result = p.map(Simulation,range(10))
result = np.array(result).reshape(...)
p.close()
p.join()

相关问题 更多 >

    热门问题