Skip to main content

Linux futex-based primitives (C extension)

Project description

fastipc — Fast Machine-level Sync for Python

Fast IPC synchronization primitives in C11/CPython extension, with:

  • Explicit acquire/release semantics and a focus on latency and throughput.
  • Primitives operate on caller-supplied buffer (thus machine-level)
  • Comes with Named helpers for easier usage out of the box.

Status: Linux-only (futex backed). Python >= 3.9. Targets x86_64 and other Linux archs.

Installation

pip install fastipc

Why fastipc

  • Minimal hot-path: pure atomics for uncontended operations; futex syscall only on contention.
  • Buffer-backed: pass a 4-byte aligned memoryview to operate in threads or across processes.
  • Strict memory ordering: acquire/release semantics on loads, stores, and CAS.
  • Simple helpers: NamedEvent, NamedMutex, NamedSemaphore for quick cross‑process usage.

Core Primitives

  • FutexWord: raw futex wait/wake on a 32‑bit word.
  • AtomicU32 / AtomicU64: atomic load/store/CAS on a shared word.
  • Mutex: futex‑based mutex with spin‑then‑sleep contention path.
  • Semaphore: futex‑based counting semaphore with exact‑delivery wakeups.

Cross‑Process: Buffer‑backed

from multiprocessing import shared_memory
from fastipc import FutexWord, Mutex, Semaphore

shm = shared_memory.SharedMemory(create=True, size=4)
try:
    fw = FutexWord(shm.buf, shared=True)
    # Other processes attach via SharedMemory(name=...) and reuse the same buffer
finally:
    shm.close(); shm.unlink()

# Mutex and Semaphore require 64 bytes
shm = shared_memory.SharedMemory(create=True, size=64)
try:
    mtx = Mutex(shm.buf)
    with mtx:
        ...  # critical section
finally:
    shm.close(); shm.unlink()

Cross‑Process: Named Helpers

These helpers use a shared‐memory word under the hood, plus a small PID‑tracking directory for safe cleanup.

from fastipc import NamedEvent, NamedMutex, NamedSemaphore

# Event
evt = NamedEvent("job_ready")
evt.set()          # wake all waiters
evt.clear()        # reset
evt.wait(1_000_000_000)  # wait with 1s timeout (ns)

# Mutex
mtx = NamedMutex("global_lock")
with mtx:
    ...

# Semaphore
sem = NamedSemaphore("queue_slots", initial=0)
sem.post(3)
sem.wait()         # blocks if no tokens

Notes:

  • PID tracking directory defaults to /dev/shm/fastipc. In restricted environments, set FASTIPC_PID_DIR=/tmp/fastipc (or any writable dir).

Performance Notes

  • Uncontended paths use only atomics (no syscalls).
  • Under contention, primitives spin briefly (adaptive) then futex sleep to minimize wake storms and context switches.
  • Semaphore.post(n) atomically adds n tokens and only wakes waiters when the count transitions from 0; the wake hint is capped at min(n, INT_MAX) to stay portable.
  • Expect on-par or better performance than posix_ipc and multiprocessing alternatives in most scenarios.

Platform Support

  • Linux only (uses linux/futex.h).
  • Wheels target manylinux/musllinux for x86_64 and aarch64. Some arches may require -latomic (handled during build).

AI Code Generation

Most of the writing of this library was assisted by AI (OpenAI Codex). The code was iteratively refined and tested to ensure correctness and performance.

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

fastipc-0.1.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distributions

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

fastipc-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

fastipc-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

fastipc-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

fastipc-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

fastipc-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file fastipc-0.1.0.tar.gz.

File metadata

  • Download URL: fastipc-0.1.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for fastipc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 040427ed5689945b563b6b6aad8ca6152bedc64c48a9b103ed7ac106b07ee8a6
MD5 0fc383cc0c5af4e41eef192ad3e56d2b
BLAKE2b-256 341471f203dc1183cf0b54327702058e658448a95ab033ec3373e0217ef50091

See more details on using hashes here.

File details

Details for the file fastipc-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fastipc-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 eb6fe9c123d4dc2a98ec0d316c38c90d9d038717aa1b2b7feb3dbb137d442f9d
MD5 2e45293e5986949356ec6c8d9626b4dd
BLAKE2b-256 f3e4b56b1f9a8c8a9e093280d2361a1cc46d54c36a7f1f64a8ad6d318b563e4c

See more details on using hashes here.

File details

Details for the file fastipc-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fastipc-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a2ea09dc192ccd7980ed3eb7d44ff2d8442366fe0405b4cf0e7bb7ad27f57f1f
MD5 cc963c636fbb717c922e7c2cc8f9df97
BLAKE2b-256 69c5153272acf4d51455a42a5c15d88d8f5b0544a537dfa0f39f1393664f5a0a

See more details on using hashes here.

File details

Details for the file fastipc-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fastipc-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 33728cd9cda160d568ebf714fa88fb411091c0bbb3d27e1bc4605ae849b8d96c
MD5 28e9f1fef29724fdfecb59a3a11a74bc
BLAKE2b-256 9db29a2c15282f65a8181d784fbaa2d3db71fcc075cf6949bc7aae3c4fc415fc

See more details on using hashes here.

File details

Details for the file fastipc-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fastipc-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4ec6f69450f173f6bac25913faca5e8192f68bda11fc2952682ba13d9eac29d6
MD5 fc3d73823f6a6983d63e5bab8d51c865
BLAKE2b-256 42867faadb1a01ad145a7713c2a01653ec4eec3e8e390584910c788278424d52

See more details on using hashes here.

File details

Details for the file fastipc-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fastipc-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 548aa251eaec0683e9caa3d4a55e26a18aa1c0226cf832521b61f17a3e96c2b6
MD5 2c555a64b95390db7c187559a27eb84a
BLAKE2b-256 be85e864f1130ad22d5596926f1268fc55457ce9296e56d93392ab9ae9b45285

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