栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否将共享的只读数据复制到不同的进程以进行多处理?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否将共享的只读数据复制到不同的进程以进行多处理?

您可以

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
,也不会复制数据,除非将其写入。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号