import multiprocessing
import numpy as np
import multiprocessing as mp
import ctypes
class Test():
def __init__(self):
shared_array_base = multiprocessing.Array(ctypes.c_double,100,lock=False)
self.a = shared_array = np.ctypeslib.as_array(shared_array_base)
def my_fun(self,i):
self.a[i] = 1
if __name__ == "__main__":
num_cores = multiprocessing.cpu_count()
t = Test()
def my_fun_wrapper(i):
t.my_fun(i)
with mp.Pool(num_cores) as p:
p.map(my_fun_wrapper,np.arange(100))
print(t.a)
在上面的代码中,我试图使用 multiprocessing
编写代码以修改数组。在每个过程中执行的函数 my_fun()
应该修改索引 a[:]
i
的值/ strong>作为参数传递给my_fun()
。关于上面的代码,我想知道正在复制什么。
1)每个进程都在复制代码中的任何内容吗?我认为对象可能是,但理想情况下什么也不是。
2)有没有一种方法可以对对象使用包装函数my_fun()
?