Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A polished shmarray, originated by David Baddeley

Project description

shmarray

NOTE: If you are using Python 3.8, please turn to multiprocessing.shared_memory instead.


shmarray is a shared numpy-array implementation utilizing multiprocessing.sharedctypes. It is a quite useful small library created of David Baddeley. I found it at https://github.com/lucastheis/parallel/blob/master/shmarray.py

Despite its usefulness, it not only does not come with a pip-installable package, but also contains disrepaired bugs.

This repo is created as a remedy.

Installation

pip install shmarray

Usage

test.py

from multiprocessing import Process
import time

import shmarray

def worker(data):
    while True:
        data += 1
        time.sleep(1)


def monitor(data):
    while True:
        print(data)
        time.sleep(0.5)

data = shmarray.zeros(10)

procs = [
    Process(target=worker, args=(data, )),
    Process(target=monitor, args=(data, )),
]


for p in procs:
    p.start()

for p in procs:
    p.join()

Result:

$ python3 test.py
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
[6. 6. 6. 6. 6. 6. 6. 6. 6. 6.]
[6. 6. 6. 6. 6. 6. 6. 6. 6. 6.]
...

Under the hood

The code in shmarray.py is mostly the same as the original, I've only done a few things:

  1. fix a bug that did not pass dtype argument to downstream calls
  2. fix python version compatibility bugs
  3. reformat the code using black

Project details


Download files

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

Files for shmarray, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size shmarray-0.0.3-py3-none-any.whl (5.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size shmarray-0.0.3.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page