Zero-copy shared memory IPC library for building complex streaming data pipelines capable of processing large datasets
Project description
MomentumX
MomentumX is a zero-copy shared memory IPC library for building complex streaming data pipelines capable of processing large datasets using Python.
Key Features:
- High-Throughput, Low Latency
- Supports streaming and synchronous modes for use within a wide variety of use cases.
- Bring your own encoding, or use raw binary data.
- Sane data protections to ensure reliability of data in a cooperative computing environment.
- Pairs with other high-performance libraries, such as numpy and scipy, to support parallel processing of memory-intensive scientific data.
- Works on most modern versions of Linux using shared memory (via
/dev/shm
). - Seamlessly integrates into a Docker environment with minimal configuration, and readily enables lightweight container-to-container data sharing.
Examples:
Below are some simplified use cases for common MomentumX workflows. Consult the examples in the examples/
directory for additional details and implementation guidance.
Stream Mode
# Producer Process
import momentumx as mx
# Create a stream with a total capacity of 10MB
stream = mx.Producer('my_stream', buffer_size=int(1e6), buffer_count=10, sync=False)
# Write the series 0-9 repeatedly to a buffer 1000 times
buffer = stream.next_to_send()
buffer.write(b'1')
# buffer data == b'1'
# alternatively, set via array indexing using python ByteArray syntax...
buffer[1] = ord('2')
# buffer data == b'12'
# or also set via python slice operator
buffer[2:3] = b'34'
# buffer data == b'1234'
buffer.send()
# NOTE: buffer.send() can also be passed an explicit number of bytes as well.
# Otherwise an internally managed cursor will be used.
# Consumer Process(es)
import momentumx as mx
stream = mx.Consumer('my_stream')
# Receive from my_stream as long as the stream has not ended OR there are unread buffers
while stream.has_next:
# Block while waiting to receive buffer
# NOTE: Non-blocking receive is possible using blocking=False keyword argument
buffer = stream.receive()
# If we are here, either the stream ended OR we have a buffer, so check...
if buffer is not None:
# We have buffer containing data, so print the entire contents
print(buffer.read(buffer.data_size))
# See also "Implicit versus Explicit Buffer Release" section below.
Sync Mode
# Producer Process
import momentumx as mx
import threading
import signal
cancel_event = threading.Event()
signal.signal(signal.SIGINT, (lambda _sig, _frm: cancel_event.set()))
# Create a stream with a total capacity of 10MB
stream = mx.Producer(
'my_stream',
buffer_size=int(1e6),
buffer_count=10,
sync=True
) # NOTE: sync set to True
min_subscribers = 1
while stream.subscriber_count < min_subscribers:
print("waiting for subscriber(s)")
if cancel_event.wait(0.5):
break
print("All expected subscribers are ready")
# Write the series 0-999 to a consumer
for n in range(0, 1000):
if stream.subscriber_count == 0:
cancel_event.wait(0.5)
# Note: sending strings directly is possible via the send_string call
elif stream.send_string(str(n)):
print(f"Sent: {n}")
# Consumer Process(es)
import momentumx as mx
stream = mx.Consumer('my_stream')
while stream.has_next:
data = stream.receive_string()
if data is not None:
# Note: receiving strings is possible as well via the receive_string call
print(f"Received: {data}")
Iterator Syntax
Working with buffers is even easier using iter()
builtin:
import momentumx as mx
stream = mx.Consumer(STREAM)
# Iterate over buffers in the stream until stream.receive() returns None
for buffer in iter(stream.receive, None):
# Now, buffer is guaranteed to be valid, so no check required -
# go ahead and print all the contents again, this time using
# the index and slice operators!
print(buffer[0]) # print first byte
print(buffer[1:buffer.data_size]) # print remaining bytes
Numpy Integration
import momentumx as mx
import numpy as np
# Create a stream
stream = mx.Consumer('numpy_stream')
# Receive the next buffer (or if a producer, obtain the next_to_send buffer)
buffer = stream.receive()
# Create a numpy array directly from the memory without any copying
np_buff = np.frombuffer(buffer, dtype=uint8)
Implicit versus Explicit Buffer Release
MomentumX Consumers will, by default, automatically release a buffer under the covers once all references are destroyed. This promotes both usability and data integrity. However, there may be cases where the developer wants to utilize a different strategy and explicity control when buffers are released to the pool of available buffers.
stream = mx.Consumer('my_stream')
buffer = stream.receive()
# Access to buffer is safe!
buffer.read(10)
# Buffer is being returned back to available buffer pool.
# Be sure you are truly done with your data!
buffer.release()
# DANGER: DO NOT DO THIS!
# All operations on a buffer after calling `release` are considered unsafe!
# All safeguards have been removed and the memory is volatile!
buffer.read(10)
Isolated Contexts
MomentumX allows for the usage of streams outside of /dev/shm
(the default location). Pass the context
kwarg pointing to a directory on the filesystem for both the Producer
and all Consumer
instances to create isolated contexts.
This option is useful if access to /dev/shm
is unsuitable.
import momentumx as mx
# Create a producer attached to the context path /my/path
stream = mx.Producer('my_stream', ..., context='/my/path/')
...
# Create Consumer elsewhere attached to the same context of /my/path
stream = mx.Consumer('my_stream', context='/my/path/')
License
Captivation Software, LLC offers MomentumX under an Unlimited Use License to the United States Government, with all other parties subject to the GPL-3.0 License.
Inquiries / Requests
All inquiries and requests may be sent to opensource@captivation.us.
Copyright © 2022-2023 - Captivation Software, LLC.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for MomentumX-2.6.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c680549b13bffe801364f07420d571fc9a623a1839847c40ff4c5554561dc8b |
|
MD5 | 6d532e63f6e7a65f3020b7c35e91ea3d |
|
BLAKE2b-256 | 1060aad990eb3eaea32ea2f20c2a6987564b03b42d4e13595ae1d6085c4d6e3b |
Hashes for MomentumX-2.6.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a37d40c945791cad0263ccfb6ac1bfeb6f5849d45bcd2b9bc03c7150ae109631 |
|
MD5 | c5e11d5bcacfa8a08adcf9210feb9727 |
|
BLAKE2b-256 | c7345293c64b59d06bd5b1b2db0b25a81562d71f191644987ee65917097b1e35 |
Hashes for MomentumX-2.6.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cde6da9ed92a54c1062430df764c84746cac49af65caf4efecb583ea207aff50 |
|
MD5 | 0bada65257da6c1a079c9b27116ada7f |
|
BLAKE2b-256 | 168ac2ad5db4037274fdde1da74fd4572cb4c7f22b73db88295df5c85c3d189a |
Hashes for MomentumX-2.6.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c879ffbf2fd19a9f4e2d7a18a7df9947d39a0c2bae19ad97eb681c5c1327c23 |
|
MD5 | b7c332e55da16ff83f6132d97cb568fe |
|
BLAKE2b-256 | fb8945fe7f487fdbb933ea655992e9ab87b48f38cd7144d4da6138fde02c9396 |
Hashes for MomentumX-2.6.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08bea730a284b57cce852e4098a16adb79b4dfcb6ea31230fe083034764084bf |
|
MD5 | 6013ad69489989fdaade94be8cfd26f7 |
|
BLAKE2b-256 | 6559476d86cb55eaa8663261cac411704391810fcd3934b7a51f57685c3f79e7 |
Hashes for MomentumX-2.6.4-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31dc0adb07efa389c6d3e5513f0888340b42baa6a6dbe7311d4e34c16652b27d |
|
MD5 | 529a65d3d689470280be20bbf9c5ac70 |
|
BLAKE2b-256 | 5fe27775515d002dd849abccb85d8bb1d759420c917bc9c0be8807396d9fb7d1 |