Skip to main content

A Python library for shared memory IPC communication

Project description

humr-ipc

A Python library for shared-memory inter-process communication (IPC). Originally developed as the IPC layer for OpenHUMR — an open-source power grid simulation platform — it has no OpenHUMR-specific dependencies and works in any Python multi-processing application that needs fast, low-overhead communication between processes.

Features

  • Shared memory transport — uses multiprocessing.shared_memory (zero-copy, no serialization overhead)
  • Async-native — built on asyncio for non-blocking send/receive
  • Pluggable middleware — swap the action handler without changing transport code
  • Pluggable bundlers — control how payloads are framed before going onto the wire
  • Cross-platform memory limits — auto-detects shared memory constraints on Windows, Linux, and WSL2
  • Enum-based message types — with automatic JSON export
  • Structured logging — colour-coded output via the built-in logger

Installation

# From PyPI:
pip install humr-ipc

# From GitHub:
pip install git+https://github.com/apteron-oss/humr-ipc.git

# For local development:
git clone https://github.com/apteron-oss/humr-ipc.git
pip install -e humr-ipc/[dev]

Quick start

Process A — creator (e.g. Python compute server)

import asyncio
from ipc import Communicator
from ipc.helpers.middleware.websocket import WebSocketMiddleware

async def handle_connection(websocket):
    middleware = WebSocketMiddleware(websocket)

    # Creates two named shared memory blocks; passes their names to Process B
    comm = Communicator(middleware, create_memory=True)

    print(comm.recv_memory_block.name)  # pass this to Process B as --recv
    print(comm.send_memory_block.name)  # pass this to Process B as --send

    await comm.listen_async()

Process B — consumer (e.g. spawned Rust/C++ binary or Python worker)

Process B attaches to the existing blocks by name:

comm = Communicator(
    middleware,
    create_memory=False,
    recv_memory_name="<name from Process A send block>",
    send_memory_name="<name from Process A recv block>",
    shared_memory_size=<size>,
)

In OpenHUMR the Rust binary reads block names from CLI arguments and uses the shared_memory crate to attach.

Architecture

Process A                           Process B
─────────────────────────────────────────────────
Communicator (creator)              Communicator (consumer)
  recv_memory_block ──────read───▶  send_memory_block
  send_memory_block ◀───write──────  recv_memory_block
  flag byte (index 0) coordinates read/write turns

Each block has a 1-byte flag at index 0. The writer sets the flag; the reader clears it after consuming. This avoids polling overhead while keeping the implementation dependency-free.

Middleware

Middleware handles what happens when a message arrives on the shared memory channel.

Class Purpose
WebSocketMiddleware Forward the payload to a WebSocket client in real time
ConsoleMiddleware Print to stdout (debugging)
BlankMiddleware No-op (testing / benchmarking)

All middleware classes implement ActionMiddleware and have a set_communicator() hook so they can send data back to the other process.

Bundlers

Bundlers frame outgoing data before writing it to shared memory.

Class Purpose
WebSocketBundler Wraps payload with action header for WebSocket routing
JSONBundler Plain JSON framing

Message actions

from ipc.helpers.enums.ipc import IPCAction

IPCAction.BYPASS    # 0 — stream raw data directly, bypass logger
IPCAction.PRINT     # 1 — log message to console
IPCAction.TERMINATE # 2 — graceful shutdown signal

Module layout

ipc/
├── communicator.py          # Communicator class — main entry point
├── helpers/
│   ├── bundler/
│   │   ├── standard.py      # JSONBundler
│   │   └── websocket.py     # WebSocketBundler
│   ├── enums/
│   │   ├── base.py          # BaseEnum with JSON export
│   │   ├── ipc.py           # IPCAction
│   │   └── websocket.py     # WebSocket constants
│   ├── middleware/
│   │   ├── blank.py         # BlankMiddleware
│   │   ├── console.py       # ConsoleMiddleware
│   │   └── websocket.py     # WebSocketMiddleware
│   ├── random/
│   │   └── uuid.py          # Timestamped UUID generation
│   └── system/
│       └── shared_memory.py # Platform shared memory limit detection
└── logger/
    └── logger.py            # Structured colour-coded logger

Requirements

  • Python 3.8+
  • No external runtime dependencies (standard library only)

Origin

humr-ipc was extracted from OpenHUMR, where it bridges the Python Starlette compute server and the Rust thermal simulation binary using named shared memory blocks. It is maintained here as a standalone package so it can be versioned and reused independently.

License

See LICENSE.

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

humr_ipc-1.0.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

humr_ipc-1.0.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file humr_ipc-1.0.0.tar.gz.

File metadata

  • Download URL: humr_ipc-1.0.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for humr_ipc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9481ad17c7989cc38cd4c8e483da51134c81557f02705fa1d6beca83583427a1
MD5 f75a345a72180c70052a7094f6e3f281
BLAKE2b-256 72d0213863f07c6a77cfacf3bd48cd18b4699fad09f33de3f5f14402c97bc762

See more details on using hashes here.

File details

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

File metadata

  • Download URL: humr_ipc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for humr_ipc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a4ad213ab611251ae03348d7a621600f02a4a84cb71b5133f99300c9bcd0044
MD5 7a41a636530815f238350d37045a5cfd
BLAKE2b-256 c9115719368162b2ab50a5a747d59469371edb1b7dfc45f8ca7e31239b2e20f2

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