Skip to main content

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 (1MB x 10)
stream = mx.Producer('my_stream', buffer_size=int(1e6), buffer_count=10, sync=False)

# Obtain the next available buffer for writing
buffer = stream.next_to_send()
buffer.write(b'1') 

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


Download files

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

Source Distribution

MomentumX-2.7.2.tar.gz (51.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

momentumx-2.7.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

momentumx-2.7.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

momentumx-2.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (226.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

momentumx-2.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (226.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

momentumx-2.7.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (226.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

MomentumX-2.7.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (225.9 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

MomentumX-2.7.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (230.7 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

MomentumX-2.7.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (230.7 kB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

File details

Details for the file MomentumX-2.7.2.tar.gz.

File metadata

  • Download URL: MomentumX-2.7.2.tar.gz
  • Upload date:
  • Size: 51.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for MomentumX-2.7.2.tar.gz
Algorithm Hash digest
SHA256 8e57d3feea3592c652b3fde4205b6aff853876db0069ea9fa85e0b365b474b49
MD5 f3d00622ae3c7494d478b14a15a74e53
BLAKE2b-256 a3fdf8dcb09aa58b7ab9a63fd7d2e512fb18713b58f03d2720d71b956b19a071

See more details on using hashes here.

File details

Details for the file momentumx-2.7.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for momentumx-2.7.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 64cffa0b3d41f6d50e657ac7abbadd31069aebb494fbfa47801ce6f3d487ae7d
MD5 9abd73318a0c7d4e3e860c105bb245c5
BLAKE2b-256 2fcd6ef3a6a32e43f633cf23bcdf8e0c94d3904d770b31f69c43c9434c50848a

See more details on using hashes here.

File details

Details for the file momentumx-2.7.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for momentumx-2.7.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f507bb1a1589287abb42b96c9e80cf79264371731438be4d48e592d30bc1fb9
MD5 86526e6c44b209562aa0c6bb1bee7fc3
BLAKE2b-256 1853cae7e464bad0bf182cb290c62d5fbb663c5133cdcf11cf0297ec50f138ff

See more details on using hashes here.

File details

Details for the file momentumx-2.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for momentumx-2.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 678b2ede1c3932881c737e50f9ebfb4e1117ae8754c5524b555be5b4980f5908
MD5 780a2bfba9f38d58b1f9c25bd1374ca4
BLAKE2b-256 4782c936c5bd2d1c15c1be103907b60c17a946d041ac08cf7d0b405fc603bb06

See more details on using hashes here.

File details

Details for the file momentumx-2.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for momentumx-2.7.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d7fa7f062abed511e5365ff0ca1b0fb90fcf520793d93e0e1b6968bb77573b9
MD5 654c3a186851f752d61a6d621f559dde
BLAKE2b-256 dc9ad45ce9fb44ec66f3e4ae35090ad95870c3d1d939a9f33570ecfc96fbcd10

See more details on using hashes here.

File details

Details for the file momentumx-2.7.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for momentumx-2.7.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2328537e45b2115941b875be44a5a4a23a14927eb632a3e591e4a1d37586bcff
MD5 d87f07b3bb1b3a9318ba2c69996d0ad6
BLAKE2b-256 142b5d91d6b720faed238a56b9d090d9a495cb0d90652ce7916da2f2d1de3cb7

See more details on using hashes here.

File details

Details for the file MomentumX-2.7.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for MomentumX-2.7.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 daeaaccb9e3596934dd349721163e759cba4a64f6976a53cd5e2803b578379c4
MD5 b38b388fefc996fddcd38853d7b555b2
BLAKE2b-256 596d66f1ce55989b7c5ffa16445935a414d9016c90d9081ae31965ef1279cb8f

See more details on using hashes here.

File details

Details for the file MomentumX-2.7.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for MomentumX-2.7.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c352822eb7b5430178248093f0b61b8eaf5b1341a36a144ec67c90eb892606b2
MD5 7844090ec3e6cd1f17ae99ff2a56578a
BLAKE2b-256 f7071f1c10f00052ff64b8a76d7a471f34e375c2d85f454db9997e02f639faea

See more details on using hashes here.

File details

Details for the file MomentumX-2.7.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for MomentumX-2.7.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cce043b6beca8c781336cfce82e964c7f5fe7d6dca16a2910748c328de5f2e89
MD5 55aac18145223f1fe295e88828496707
BLAKE2b-256 e98f2734b3989cae9cb2c693eb5942e3a29fcdbae3a2764745b54329835c8de1

See more details on using hashes here.

Supported by

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