Share numpy arrays between processes
This is a simple python extension that lets you share numpy arrays with other processes on the same computer. It uses either shared files or POSIX shared memory as data stores and therefore should work on most operating systems.
Here’s a simple example to give an idea of how it works. This example does everything from a single python interpreter for the sake of clarity, but the real point is to share arrays between python interpreters.
import numpy as np import SharedArray as sa # Create an array in shared memory. a = sa.create("shm://test", 10) # Attach it as a different array. This can be done from another # python interpreter as long as it runs on the same computer. b = sa.attach("shm://test") # See how they are actually sharing the same memory. a = 42 print(b) # Destroying a does not affect b. del a print(b) # See how "test" is still present in shared memory even though we # destroyed the array a. This method only works on Linux. sa.list() # Now destroy the array "test" from memory. sa.delete("test") # The array b is still there, but once you destroy it then the # data is gone for real. print(b)
- Python 2.7 or 3+
- Numpy 1.8+
- Posix shared memory interface
SharedArray uses the posix shm interface (shm_open and shm_unlink) and so should work on most POSIX operating systems (Linux, BSD, etc.).
The extension uses the distutils python package that should be familiar to most python users. To test the extension directly from the source tree, without installing, type:
python setup.py build_ext --inplace
To build and install the extension system-wide, type:
python setup.py build sudo python setup.py install
The package is also available on PyPI and can be installed using the pip tool.
On Linux, I get segfaults when working with very large arrays.
A few people have reported segfaults with very large arrays using POSIX shared memory. This is not a bug in SharedArray but rather an indication that the system ran out of POSIX shared memory.
On Linux a tmpfs virtual filesystem is used to provide POSIX shared memory, and by default it is given only about 20% of the total available memory, depending on the distribution. That amount can be changed by re-mounting the tmpfs filesystem with the size=100% option:
sudo mount -o remount,size=100% /run/shm
Also you can make the change permanent, on next boot, by setting SHM_SIZE=100% in /etc/defaults/tmpfs on recent Debian installations.
I can’t attach old (pre 0.4) arrays anymore.
Since version 0.4 all arrays are now page aligned in memory, to be used with SIMD instructions (e.g. fftw library). As a side effect, arrays created with a previous version of SharedArray aren’t compatible with the new version (the location of the metadata changed). Save your work before upgrading.
Packages are also available on PyPi at: https://pypi.python.org/pypi/SharedArray
For bug reports, feature requests, suggestions, patches and everything else related to SharedArray, feel free to raise issues on the project page. You can also contact the maintainer directly by email at email@example.com.