Skip to main content

spsc-ring-threadsafe

Lockless, thread-safe, single-producer, single-consumer, FIFO queue for Python — implemented on a ring buffer in C.

PyPI Version PyPI Status PyPI Wheel PyPI Implementation PyPI License PyPI Downloads

Design

Built on a Lamport ring buffer with C11 _Atomic read/write indexes (acquire/release ordering). Producer and consumer never contend with eachother.

Features:

  • Single producer, single consumer, FIFO semantics
  • Lockless (no mutexes or spinlocks)
  • Non-blocking / async-friendly
  • Suitable for shared memory and IPC
  • Compatible with no-GIL & subinterpreters
  • Low overhead, especially for small messages
  • Up to 100x faster than multiprocessing.Queue in the standard library

Benchmarks

spsc-ring-threadsafe_benchmarks


Quick Start

import spsc_ring_threadsafe as srt

buf = bytearray(4096)
srt.init(buf)  # initialize as ring buffer

item = b"this is a bytestring message"
srt.put(buf, item)

result = srt.get(buf)
print(result.decode())

Output:

this is a bytestring message

Shared Memory

from multiprocessing import shared_memory
import spsc_ring_threadsafe as srt


shm_name = "app_123456"

# Process 1 (producer)
a = shared_memory.SharedMemory(create=True, size=4096, name=shm_name)
srt.init(a.buf) # initialize as ring buffer

item = b"hello from shared memory!"
srt.put(a.buf, item)
a.close()


# Process 2 (consumer)
b = shared_memory.SharedMemory(name=shm_name)
result = srt.get(b.buf)
print(result.decode())

b.close()
b.unlink()

Output:

hello from shared memory!

API Reference

Exceptions

Exception Raised when
spsc_ring_threadsafe.QueueFullError put() called on a full buffer
spsc_ring_threadsafe.QueueEmptyError get() called on an empty buffer

Functions

spsc_ring_threadsafe.init(buf)

Initialize a mutable buffer as a ring buffer (this function sets the read + write indexes to zero).

Parameter Description
buf Mutable buffer-compatible object (bytearray, memoryview, etc.). Size must be a power-of-two anywhere from 256 bytes to 2 GiB. Also accepts shared memory buffers.

⚠️ Thread safety: Initialization is not thread-safe. Initialize once before any concurrent access, or provide your own synchronization.


spsc_ring_threadsafe.put(buf, item)

Insert an item into the ring buffer. Non-blocking.

Parameter Description
buf Mutable buffer-compatible object. Size must be a power-of-two anywhere from 256 bytes to 2 GiB. Also accepts shared memory buffers.
item Buffer-compatible object to insert

Raises: QueueFullError if the buffer has insufficient space.

⚠️ Buffer must be zeroed or initialized before use.

⚠️ Thread safety: Only a single producer is allowed for a given queue! Multiple producers are NOT thread-safe.


spsc_ring_threadsafe.get(buf)

Remove and return an item from the ring buffer. Non-blocking.

Parameter Description
buf Mutable buffer-compatible object. Size must be a power-of-two anywhere from 256 bytes to 2 GiB. Also accepts shared memory buffers.

Returns: Buffer-compatible object containing the message.

Raises: QueueEmptyError if no message is available.

⚠️ Buffer must be zeroed or initialized before use.

⚠️ Thread safety: Only a single consumer is allowed for a given queue! Multiple consumers are NOT thread-safe.


Installation

uv add spsc-ring-threadsafe

or

pip install spsc-ring-threadsafe

Development

Run tests

uv run python -m unittest discover -s tests -v

Run benchmarks

uv pip install -e . --force-reinstall --no-deps
uv run python tests/benchmark.py

Run dev script

uv pip install -e . --force-reinstall --no-deps
uv run python tests/dev.py

LLM Usage Disclosure

Tests and benchmarks were written with assistance from AI models. All C code is written by hand.

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

spsc_ring_threadsafe-0.0.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (18.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file spsc_ring_threadsafe-0.0.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: spsc_ring_threadsafe-0.0.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Linux Mint","version":"22.3","id":"zena","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for spsc_ring_threadsafe-0.0.9-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c7b032e5c68a99ac96621adacb31e8e3354f38c1e30543df53486e664998fa97
MD5 dd762f26dbfce3290d54c31afa1d169f
BLAKE2b-256 176eac59595c5fcb7884382a4a8cd52ba0b38151295c7149cc9554386b2722e6

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.9 This release

1 file

0.0.8

1 file

0.0.7

1 file

0.0.6

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page