Skip to main content

Sharing numpy ndarray with a simple API between different python processes with shared memory.

Project description

Overview

ndarray + sharing = ndsharray

Introduction

This python module let you share a numpy ndarray within different processes (either via python's multiprocessing or sharing between different python instances). The library behind this package is the lib mmap from official python - no extra library, except numpy, is needed. The mmap approach is using the shared memory, which can be accessed by different CPUs/python instances. Using shared memory is much faster than the pickle approach - you can even do a video streaming on a Raspberry Pi / Jetson Nano over multiple python processes.
This library is easy-to-use, just initialize the shared array with a unique tag and write/read! You can even change the numpy array dimension/shape/dtype during runtime - the mmap will be silently rebuild if there is a change in the numpy array size/shape/dtype.

Small Example Code:

import numpy as np
from ndsharray import NdShArray
    
shared_array = NdShArray("my_unique_tag", r_w='w')  # r_w must be specified

my_array = np.random.random((400, 800))
shared_array.write(my_array)

print(my_array)

That's all for writing into shared memory. How to read? Open a second python instance:

import numpy as np
from ndsharray import NdShArray

shared_array = NdShArray("my_unique_tag", r_w='r')  # r_w must be specified

status, my_array = shared_array.read()

print(my_array)

Documentation

Can be found here: https://ndsharray.readthedocs.io/en/latest/

Requirements

  • Python ≥ 3.6
  • numpy

Tested with example codes on

  • Windows 10, amd64, Python 3.6 / 3.8
  • Ubuntu 20, amd64, Python 3.6 /3.8
  • NVIDIA Jetson Nano with Ubuntu 18.04, ARM64-bit (aarch64), Python 3.6

Some technical notes

This library shall be an easy-to-use library and also shall be faster than pickling numpy arrays to another process. Please note that the python's provided shared_memory does the same as ndsharray, but is using byte array instead of numpy array! However, shared_memory is available since python 3.8 and not supported for python 3.6.

Installation from Github

Make sure to have git, python and pip in your environment path or activate your python environment.
To install enter in cmd/shell:

git clone https://github.com/monzelr/ndsharray.git

cd ndsharray

pip install .

Alternative with python:

python setup.py install

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ndsharray-1.1.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file ndsharray-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: ndsharray-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for ndsharray-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2a8e1fa5328eb160b5bf34f7bf433f91d520ef9b7780707911fe26a5689f84a7
MD5 0bf7b3c5776aca6c5bae1c54a339594b
BLAKE2b-256 8ccf1a48fc3263cd66150425fe3b09e3c2ea07e57403398b52c5d654c4d8b129

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page