I would like to be able to send shared arrays to processes. I'm looking for a way which enables me to re-use processes when the shape of the shared arrays keeps changing. The code should be able to run interactively, hence I prefer to avoid Pool. Unfortunately it seems I cannot send RawArray objects using multiprocessing.Pipe. Is there some way to avoid this problem?
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!"
This results in:
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