このウェブサイトのどこかで次の例を見つけました。
import multiprocessing
import ctypes
import numpy as np
shared_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)
# No copy was made
assert shared_array.base.base is shared_array_base.get_obj()
# Parallel processing
def my_func(i, def_param=shared_array):
shared_array[i,:] = i
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
pool.map(my_func, range(10))
print shared_array
上記のコードは問題なく動作しますが、共有配列に配列を追加したい場合は、shared_array += some_other_array (上記の shared_array[i,;] = i の代わりに) のようなものを取得します。
代入前に参照されるローカル変数 'shared_array'
なぜ私はそれができないのですか?