Shared-memory objects in multiprocessing
multiprocessing, numpy, parallel-processing, python, shared-memory
Solution
If you use an operating system that uses copy-on-write `fork()` semantics (like any common unix), then as long as you never alter your data structure it will be available to all child processes without taking up additional memory. You will not have to do anything special (except make absolutely sure you don't alter the object).
The most efficient thing you can do for your problem would be to pack your array into an efficient array structure (using `numpy` or `array`), place that in shared memory, wrap it with `multiprocessing.Array`, and pass that to your functions. This answer shows how to do that.
If you want a writeable shared object, then you will need to wrap it with some kind of synchronization or locking. `multiprocessing` provides two methods of doing this: one using shared memory (suitable for simple values, arrays, or ctypes) or a `Manager` proxy, where one process holds the memory and a manager arbitrates access to it from other processes (even over a network).
The `Manager` approach can be used with arbitrary Python objects, but will be slower than the equivalent using shared memory because the objects need to be serialized/deserialized and sent between processes.
There are a wealth of parallel processing libraries and approaches available in Python. `multiprocessing` is an excellent and well rounded library, but if you have special needs perhaps one of the other approaches may be better.
Problem
Suppose I have a large in memory numpy array, I have a function `func` that takes in this giant array as input (together with some other parameters). `func` with different parameters can be run in parallel. For example: ``` def func(arr, param): # do stuff to arr, param # build array arr pool = Pool(processes = 6) results = [pool.apply_async(func, [arr, param]) for param in all_params] output = [res.get() for res in results] ``` If I use multiprocessing library, then that giant array will be copied for multiple times into different processes. Is there a way to let different processes share the same array? This array object is read-only and will never be modified. What's more complicated, if arr is not an array, but an arbitrary python object, is there a way to share it? [EDITED] I read the answer but I am still a bit confused. Since fork() is copy-on-write, we should not invoke any additional cost when spawning new processes in python multiprocessing library. But the following code suggests there is a huge overhead: ``` from multiprocessing import Pool, Manager import numpy as np; import time def f(arr): return len(arr) t = time.time() arr = np.arange(10000000) print "construct array = ", time.time() - t; pool = Pool(processes = 6) t = time.time() res = pool.apply_async(f, [arr,]) res.get() print "multiprocessing overhead = ", time.time() - t; ``` output (and by the way, the cost increases as the size of the array increases, so I suspect there is still overhead related to memory copying): ``` construct array = 0.0178790092468 multiprocessing overhead = 0.252444982529 ``` Why is there such huge overhead, if we didn't copy the array? And what part does the shared memory save me?