Reputation: 478
I want to share a multiprocessing.Array
between the parent and its child processes:
self.procs = [MP.Process(target=worker, kwargs=dict(sh_array=array)
for _ in range(num_workers)]
Does the code above do the right thing? I only want fast IPC communication based on shared memory / file-mapping when I access the shared array. I don't want any message passing or copy-based IPC happening behind the curtain. That would defeat the purpose of the code I'm writing.
Also, I'd like to pass the same way different instances of a class which all refer to the same shared array. Will this work correctly or should I pass the shared array separately and then rebuild the objects in the child processes manually?
Upvotes: 1
Views: 191
Reputation: 213837
Does the code above do the right thing?
Yes, that's exactly what multiprocessing.Array
is for and how it is used.
I don't want any IPC communication when I access the shared array.
I don't think the term "IPC" is used correctly here. IPC stands for inter-process communication, and if you have an array that is shared between processes, then anything you write to the array will be available to be read from the other processes. In other words, you are communicating between processes. In other words, IPC. Shared memory is IPC, and if you don't want IPC, then you can't share things between processes.
You may mean something completely different. Maybe you don't want to pass messages back and forth, or something like that?
Also, I'd like to pass the same way different instances of a class which all refer to the same shared array. Will this work correctly or should I pass the shared array separately and then rebuild the objects in the child processes manually?
Either way works. Do whichever option makes the code more natural to read.
Upvotes: 1