我希望能够将共享数组发送到进程。我正在寻找一种方法,使我能够在共享数组的形状不断变化时重用进程。代码应该能够以交互方式运行,因此我更愿意避免使用Pool。不幸的是,我似乎无法使用multiprocessing.Pipe发送RawArray对象。有没有办法避免这个问题?
import multiprocessing as mp
from pylab import *
N = 100
Z = mp.RawArray('d', randn(100))
def process_tasks(conn):
while True:
task = conn.recv()
if task is False:
conn.send(True)
else:
new_Z = task[0]
print sum(array(new_Z))
conn.send(True)
home_pipe, away_pipe = mp.Pipe()
my_proc = mp.Process(target=process_tasks, args=(away_pipe,))
my_proc.start()
home_pipe.send((Z,))
if home_pipe.recv():
print "Done!"
这导致:
PicklingError Traceback (most recent call last)
<ipython-input-4-0c6a5494376a> in <module>()
20 my_proc.start()
21
---> 22 home_pipe.send((Z,))
23
24 if home_pipe.recv():
PicklingError: Can't pickle <class 'multiprocessing.sharedctypes.c_double_Array_100'>: attribute lookup multiprocessing.sharedctypes.c_double_Array_100 failed