Skip to main content

Package for communication with RabbitMQ

Project description

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

Project details


Download files

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

Source Distribution

Built Distribution

File details

Details for the file double-buffer-shared-memory-manager-1.0.0.tar.gz.

File metadata

File hashes

Hashes for double-buffer-shared-memory-manager-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fe6efba00e4f0165adbe241845f027dd66747872ebba9471b614ae9dec16e7fb
MD5 36c4709cf2997a5b9ad40229ec9af860
BLAKE2b-256 da4c654871de0e90a94f069a298e3955521495fa37ecfae0677560154849ca84

See more details on using hashes here.

File details

Details for the file double_buffer_shared_memory_manager-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for double_buffer_shared_memory_manager-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4eed1707774392fb5f8c547bfcca8f92aa395adaa1c0f1db94deb57dbbb8809
MD5 7418e4df92d099905c6b39dc25ba42e0
BLAKE2b-256 fc0bbf21905968185e6e05026f316da9ed7f1cff4c069d71c9c1a18dffa1742b

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