Package: npshmex
npshmexNpshmex provides a drop-in replacement for concurrent.futures.ProcessPoolExecutor,
using shared memory provided bySharedArray(rather than pickle) to transfer numpy arrays between processes.Synopsis:importnumpyasnpfromnpshmeximportProcessPoolExecutordefadd_one(x):returnx+1ex=ProcessPoolExecutor()big_data=np.ones(int(2e7))f=ex.submit(add_one,big_data)print(f.result()[0])# 2.0The last two lines take about ~290 ms on my laptop, but ~1250 ms usingconcurrent.futures.ProcessPoolExecutor: more than a factor four difference.
To run this example, npshmex claims only half as much memory as the standard libraryProcessPoolExecutor, based on the threshold at which I get a MemoryError.For the trivialadd_onetask, multiprocessing overhead is dominant even when
spawning a single child process (a bareadd_one(big_data)takes ~55 ms).
However, since part of the multiprocessing overhead is in the parent process,
it will also bottleneck more complex tasks when they are scaled over enough processes.How it worksPython multiprocessing uses pickle to serialize data for transfer between processes.
When passing around large numpy arrays, this can quickly become a bottleneck.Npshmex's ProcessPoolExecutor-replacement instead transfers input and output numpy arrays
using shared memory (/dev/shm).
Dictionary outputs with numpy arrays as values are also supported.
Only the shared-memory `filenames' are actually transferred between processes.Note that npshmex copies data from numpy arrays into shared memory
to transfer them. It doesn't copy it again on retrieval; it just creates the
numpy array with the shared memory backing it.
Still, if you are transferring the same array back and forth,
this amounts to two unnecessary memory copies.
You can avoid these, and the use of npshmex, by managing the shared memory yourself:fromconcurrent.futuresimportProcessPoolExecutorimportSharedArraydefadd_one(shm_key):x=SharedArray.attach(shm_key)x+=1shm_key='shm://test'ex=ProcessPoolExecutor()big_data=SharedArray.create(shm_key,int(2e7))big_data+=1f=ex.submit(add_one,shm_key)f.result()SharedArray.delete(shm_key)print(x[0])# 2.0The last four lines now only take ~130 ms on my laptop, which is over
twice as fast as npshmex. However, as you can see, it involves
a more substantial rewrite of your code.Npshmex also supports numpy structured arrays: it transfers the content as anp.voidarray,
and the dtype (encoded to bytes) using a separate array.Clearing shared memoryNpshmex tells SharedArray to mark shared memory for deletion as soon as it has created
numpy arrays back from it. As explained in theSharedArraydocumention,
you'll keep the numpy array until you lose the last reference to it
(as with regular python objects).If your program exits while data is being transfered between processes,
some shared files will remain in/dev/shm. You can manually clear all npshmex-associated
shared memory from all processes on the machine withnpshmex.shm_clear().
Otherwise, it will be up to you, your operating system, or your system administrator
to clean up the mess...Array wrappersYou can also use npshmex for objects that wrap numpy arrays.
For example:importnumpyasnpclassWrapper:wrapped_array:np.ndarrayother_field='whatever'...fromnpshmeximportregister_array_wrapperregister_array_wrapper(Wrapper,'wrapped_array')Whenever your functions take or return a Wrapper, the wrapped array will be passed through shared memory without pickling it first.
The rest of the wrapper will be pickled.v0.2.1Fix cleanup of array-wrapper objectsv0.2.0Support for registering array-wrapping objectsv0.1.0Initial release