如何从多处理池映射返回numpy数组?

时间:2017-05-22 17:22:31

标签: python python-2.7 numpy multiprocessing python-multiprocessing

我有一个功能" func"接收一个列表,其中包含名为" matrix_image":

的图像行数
layoutIfNeeded()

然后,计算在func内完成,我得到一个代表图像的结果矩阵的新行。

list_rows = range(N_rows)

" NEW_ROW"在def func(list_rows): new_row = numpy.empty(N_columns) .... ....# some computation .... return new_row matrix_image = pool.map(func, list_n_rows ) 内正确计算,因为我在调试器中看到了值,但结果matrix_image的形状为:(list_n_rows,)。 填写的所有值。

如何从func返回(1D numpy数组)以附加到结果矩阵(2D numpy数组)?

1 个答案:

答案 0 :(得分:2)

您可以使用多重处理的RawArray功能,在此过程中,您需要定义需要从流程中访问的变量,然后才能将其作为RawArray启动,然后在流程完成访问后作为重塑的numpy数组。

这里是一个例子:

import numpy as np
import multiprocessing as mp

n_elements = 1000 # how many elements your numpy should have

def myProc( shared_var ):
    '''
    here you convert your shared variable from mp.RawArray to numpy
    then treat it as it is numpy array e.g. fill it in with some 
    random numbers for demonstration purpose
    '''
    var = np.reshape( np.frombuffer( shared_var, dtype=np.uint32 ), -1 )
    for i in range( n_elements ):    
        var[i] = np.random.randint( 0, 2**16, 1 )
    print( 'myProc var.mean() = ', var.mean() )               

#buffer that contains the memory
mp_buff = mp.RawArray( 'i', n_elements )
p = mp.Process( target=myProc, args=(mp_var,) )
p.start()                                      
p.join()

#after the process has ended, you convert the buffer that was passed to it
var = np.reshape( np.frombuffer( mp_var, dtype=np.uint32 ), -1)
#and again, you can treat it like a numpy array
print( '   out var.mean() = ',var.mean() )                                            

输出为:

myProc var.mean() =  32612.403
       var.mean() =  32612.403

希望有帮助!

请注意,如果您从并发进程访问此缓冲区,则需要组织适当的锁定机制,这样就不会有两个进程同时修改同一块内存。