您可以
multiprocessing轻松地将共享内存与Numpy一起使用:
import multiprocessingimport ctypesimport numpy as npshared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())shared_array = shared_array.reshape(10, 10)#-- edited 2015-05-01: the assert check below checks the wrong thing# with recent versions of Numpy/multiprocessing. That no copy is made# is indicated by the fact that the program prints the output shown below.## No copy was made##assert shared_array.base.base is shared_array_base.get_obj()# Parallel processingdef my_func(i, def_param=shared_array): shared_array[i,:] = iif __name__ == '__main__': pool = multiprocessing.Pool(processes=4) pool.map(my_func, range(10)) print shared_array
哪个打印 ``
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4. 4. 4. 4. 4. 4.] [ 5. 5. 5. 5. 5. 5. 5. 5. 5. 5.] [ 6. 6. 6. 6. 6. 6. 6. 6. 6. 6.] [ 7. 7. 7. 7. 7. 7. 7. 7. 7. 7.] [ 8. 8. 8. 8. 8. 8. 8. 8. 8. 8.] [ 9. 9. 9. 9. 9. 9. 9. 9. 9. 9.]]
但是,Linux在上具有写入时复制的语义
fork(),因此,即使不使用
multiprocessing.Array,也不会复制数据,除非将其写入。



