Skip to main content

A thread-safe SPSC queue, implemented on a ring buffer written in C

Project description

Lockless thread-safe SPSC queue for Python, implemented on a ring buffer written in C

spsc-ring-threadsafe is a C extension for Python implementing a lockless, threadsafe FIFO queue.

Design

Lamport ring buffer with C11 _Atomic read + write indexes (acquire + release). Producer and consumer never contend with eachother.

Features:

  • single producer, single consumer FIFO semantics
  • lockless
  • non-blocking / async friendly
  • suitable for shared memory and IPC
  • compatible with no-GIL
  • compatible with subinterpreters
  • low overhead, especially for small messages
  • up to 100x performance when compared to multiprocessing.Queue in the standard library

Benchmarks

spsc-ring-threadsafe_benchmarks

Example

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

API

Exceptions

spsc_ring_threadsafe.QueueFullError

spsc_ring_threadsafe.QueueEmptyError

Functions

spsc_ring_threadsafe.init(buf)

buf: mutable buffer-compatible object containing the ring buffer (e.g. bytearray, memoryview). Size must be a power of two, and between 256 and 2GiB.

WARNING: initializing a buffer is not threadsafe while the ring buffer is being read / written to concurrently. Initialize once beforehand, or use your own locking primitives.

spsc_ring_threadsafe.put(buf, item)

buf: mutable buffer-compatible object containing the ring buffer (e.g. bytearray, memoryview). Size must be a power of two, and between 256 and 2GiB. item: buffer-compatible object to insert into the ring buffer.

This function does not block. If there is no space in the buffer for the inserted value, spsc_ring_threadsafe.QueueFullError will be raised immediately.

WARNING: buffer must be zeroed or initialized before use

spsc_ring_threadsafe.get(buf)

buf: mutable buffer-compatible object containing the ring buffer (e.g. bytearray, memoryview). Size must be a power of two, and between 256 and 2GiB.

Returns: a buffer-compatible object.

This function does not block. If there is no message available in the ring buffer, spsc_ring_threadsafe.QueueEmptyError will be raised immediately.

WARNING: buffer must be zeroed or initialized before use

Installation

uv add spsc-ring-threadsafe

or pip install spsc-ring-threadsafe

Run Tests

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

Run benchmark

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

Run dev script for testing

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

LLM usage disclosure

Test & benchmarks were written with assistance from AI models. All C code is written by hand.

Project details


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.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (17.8 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.2-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.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 17.8 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.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 859cd5c21479ca435378fffd847c1150dd7aff4c6ee884fa6ddcf399b05ff62e
MD5 6501a1eb624e2ca6a81160714fd6bbde
BLAKE2b-256 a4b31698a15f86d3214ab0ace07bdc4bfd2a0b4bf009d8154379dd3934770986

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