所以我有这个基本上对数组求和的程序。我使用了多处理,因为程序必须分析700,000行左右。下面我制作了一个小脚本,说明了我遇到的问题
import multiprocessing as mp
import numpy as np
import ctypes
t = range(100000)
list_1 = np.zeros((10))
for i in t:
list_1 += np.array([1]*10)
list_t = mp.Array(ctypes.c_double, 10)
list_2 = np.ctypeslib.as_array(list_t.get_obj())
list_2 = list_2.reshape(10)
# No copy was made
assert list_2.base.base is list_t.get_obj()
def proc(x):
global list_2
list_2 += np.array([1]*10)
if __name__ == '__main__':
pool = mp.Pool(processes = 6)
pool.map(proc,t)
其输出:
In [2]: list_1
Out[2]:
array([ 100000., 100000., 100000., 100000., 100000., 100000.,
100000., 100000., 100000., 100000.])
In [3]: list_2
Out[3]:
array([ 55828., 65441., 99300., 68068., 78774., 78514., 82393.,
83446., 82854., 86987.])
显然我希望两个数组都像list_1一样。这里发生了什么?