Skip to main content

DoubleBufferedSharedMemoryManager

DoubleBufferedSharedMemoryManager is a Python class designed to manage double-buffered shared memory for concurrent read/write operations, particularly suited for handling video frames stored as NumPy ndarrays.

Features

  • Double-Buffered Memory: Allows one buffer to be read while the other is being written to, minimizing read-write conflicts.

  • Thread-Safe Access: Utilizes reader-writer locks to allow multiple readers and a single writer.

  • Dynamic Memory Management: Dynamically creates or connects to shared memory segments based on the provided base name.

  • Customizable Data Handling: Supports specifying the shape and data type of the NumPy ndarray.

Requirements

  • Python 3.x

  • NumPy

  • multiprocessing

  • readerwriterlock

  • colorlog (for enhanced logging with color)

Installation

Ensure you have the required libraries:

pip install numpy readerwriterlock colorlog

Usage

Initialization

Import and initialize the manager:

from double_buffered_shared_memory import DoubleBufferedSharedMemoryManager
import numpy as np

# Initialize with default parameters
manager = DoubleBufferedSharedMemoryManager('image_buffer')

# Initialize with custom shape and dtype
manager = DoubleBufferedSharedMemoryManager('image_buffer', shape=(576, 720, 3), dtype=np.uint16)

Writing to Shared Memory

# Create a random HD image
img = np.random.randint(255, size=(1080, 1920, 3), dtype=np.uint8)

# Write to the active buffer
manager[0] = img

0 is a convention, you can use any number, but for clarity use 0

Reading from Shared Memory

# Switch the active buffer
manager.__switch()

# Read from the non-active buffer
img_copy = manager[0]

0 is a convention, you can use any number, but for clarity use 0

Cleanup

It’s important to clean up shared memory resources when they’re no longer needed:

manager.cleanup()

Using with Context Manager

The class supports context management protocol for automatic resource management:

with DoubleBufferedSharedMemoryManager('image_buffer') as manager:
    # Your code to use the manager
    pass

Logging

The class utilizes colorlog for enhanced logging. Log messages will vary in color based on the log level to provide a clearer and more intuitive understanding of the operations and events.

Contributing

Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.

Author

Massimo Ghiani m.ghiani@gmail.com

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Metadata

Release files for double-buffer-shared-memory-manager 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for double-buffer-shared-memory-manager 1.0.3
File Size Uploaded Provenance
double-buffer-shared-memory-manager-1.0.3.tar.gz 7.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for double-buffer-shared-memory-manager 1.0.3
File Interpreter ABI Platform
double_buffer_shared_memory_manager-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / double-buffer-shared-memory-manager-1.0.3.tar.gz

Download URL double-buffer-shared-memory-manager-1.0.3.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b146cbf2cd8dc8d3f103b68a139ecb5bb59a0755052f44ac88b7b849d8b04e7f
BLAKE2b-256 checksum
How to use checksums
51634bcfc9a2d506b55765015a4c5bf982e4fcb1f8fe9cbaf86dc2d3bcb4d64d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / double_buffer_shared_memory_manager-1.0.3-py3-none-any.whl

Download URL double_buffer_shared_memory_manager-1.0.3-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e64bd2a2c381af21484d0f24657303f32f3c2895b030695f915b10ee2857716
BLAKE2b-256 checksum
How to use checksums
1d7977a3d31797ddcd6f69145b07e58fb8d0989152ffac5d3343d958a7e8f25f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for double-buffer-shared-memory-manager 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page