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.

Metadata

Release files for spsc-ring-threadsafe 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spsc-ring-threadsafe 0.1.0
File Size Uploaded Provenance
spsc_ring_threadsafe-0.1.0.tar.gz 9.3 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for spsc-ring-threadsafe 0.1.0
File
spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ x86-64, Linux glibc 2.17+ x86-64 Details
spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details
spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.17+ x86-64 Details
spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details

Total release size: 90.4 kB

Release files / spsc_ring_threadsafe-0.1.0.tar.gz

Download URL spsc_ring_threadsafe-0.1.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
02e9cf3f3dd88d1f12b57a75c2fc8d70fd5b7b6c5d04e7b1dd9238d4d6c6100a
BLAKE2b-256 checksum
How to use checksums
674c347f4a5768b1b17103c834323e3411459d63b28ab08beb9a357311cefc6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"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}

Release files / spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 20.1 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
797bd1c49fad68c87200f7caad33279561bca64d13a6cc09f754983ac8ccfdea
BLAKE2b-256 checksum
How to use checksums
073a1fd2ed0417815428e93a0f967863d370ed5e1f368dd9290823c718199b18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"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}

Release files / spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL spsc_ring_threadsafe-0.1.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 21.2 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
f5b9b6c8271343e99995c5bdb0d632b7a6e4fff4b61d9ecb7672b0421a4d4916
BLAKE2b-256 checksum
How to use checksums
628a4484555076a9422ffbd3dfa3356e87aee047381e66b278ede3b7bde99b03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"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}

Release files / spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 19.4 kB
Tags CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ba8406b48e1dfe94d1c267e6209a371fbca3adde6a829f06d2f127e334087808
BLAKE2b-256 checksum
How to use checksums
4844cc39fd42ed09b27f8f9cfd20b5b95b497819e632bd0e88210573553e9d93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"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}

Release files / spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL spsc_ring_threadsafe-0.1.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 20.3 kB
Tags CPython 3.14 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
b7b529bcba9d905553eddd6c181051cd65896b01cc78dd41393aac31430d5b5d
BLAKE2b-256 checksum
How to use checksums
1e1583bfa3017d098835118701940757fa7dcb0c173d981841b24565e0fd1931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"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}

Security for spsc-ring-threadsafe 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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