Skip to main content

A lock-free, high-concurrency task broker for Python, powered by Rust.

Project description


Logo

Pyroxide

A lock-free, high-concurrency background task broker for Python, powered by Rust.

Rust Python License: MIT/Apache-2.0/Coffee

Explore the Docs »

API Reference · See Examples · Report Bug · Request Feature


Pyroxide (pyro3) is a lightweight, ultra-high-performance background task broker designed to bridge Python and Rust. It allows CPU-bound or blocking workloads to bypass the Python Global Interpreter Lock (GIL) with minimal memory overhead and zero CPU-sleep polling.

Why Pyroxide?

  • 🚀 Bypass the GIL (GIL-Free): Execute CPU-intensive compiled tasks on background OS threads without holding the Python GIL.
  • Microsecond Latency: Utilizes OS-level signaling (Condvar) rather than CPU-burning thread polling, dispatching and completing tasks in under 25 microseconds.
  • 📦 Zero Infrastructure: Runs completely in-process. No Redis, RabbitMQ, or Celery worker daemons to configure or maintain.
  • 💾 Zero-Copy Serialization: Pass large byte arrays, memoryviews, or columnar buffers across the C-ABI boundary without copy or pickle overhead.
  • 🛠️ On-the-Fly Native Compilers: Write code as Python strings and compile them to dynamic libraries on-the-fly (Rust, C, and Zig supported!).

Pyroxide vs. Alternatives

Feature / Metric Pyroxide Threading (std) Multiprocessing Celery / RQ
GIL Bypass ✅ Yes (WASM/dylib) ❌ No ✅ Yes ✅ Yes
IPC / Serialization ✅ None (Shared Memory) ✅ None ❌ High (Pickling) ❌ High (Network/Redis)
Infrastructure ✅ None (Embedded) ✅ None ⚠️ Low (Spawns processes) ❌ High (Redis/RabbitMQ)
Best For 🔥 High-perf in-process pipelines I/O-bound Python CPU-heavy Python Distributed tasks

For a detailed analysis, check out the Library Comparison Guide.


Installation

From PyPI

pip install pyro3

Build Locally

Ensure you have Rust, Python (3.8+), and maturin installed:

git clone https://github.com/emivvvvv/pyroxide.git
cd pyroxide
pip install maturin
maturin develop

Quick Start

1. Offload Python Callables

from pyroxide import task

@task
def calculate_square(x: int) -> int:
    return x * x # Runs in background OS threads

# Submit and get a handle immediately
handle = calculate_square(12)
result = handle.result() # Blocks natively (0% CPU) until complete
print(result) # 144

2. Sandboxed WebAssembly (GIL-Free)

Run computations GIL-free in a secure, virtual sandbox without compiling native code:

from pyroxide import register_wasm, wasm_task

# 1. Register WebAssembly bytecode
with open("rot13.wasm", "rb") as f:
    register_wasm("rot13", f.read())

# 2. Decorate stub function
@wasm_task("rot13")
def rot13_cipher(payload: str) -> str:
    pass

# 3. Execute GIL-free on the Rust worker pool!
print(rot13_cipher("hello").result()) # "uryyb"

3. Dynamic Shared Libraries (On-the-Fly Compilation)

Compile and load native code strings on-the-fly. Rust (compile_dylib), C (compile_c), and Zig (compile_zig) are supported:

from pyroxide import compile_dylib, dylib_task

RUST_SRC = """
#[no_mangle]
pub unsafe extern "C" fn pyroxide_plugin_run(ptr: *const u8, len: usize, out_len: *mut usize) -> *mut u8 {
    let input = std::slice::from_raw_parts(ptr, len);
    let s = std::str::from_utf8(input).unwrap_or("");
    let result = s.to_uppercase().into_bytes();
    *out_len = result.len();
    let boxed = result.into_boxed_slice();
    Box::into_raw(boxed) as *mut u8
}

#[no_mangle]
pub unsafe extern "C" fn pyroxide_plugin_free(ptr: *mut u8, len: usize) {
    let _ = Box::from_raw(std::slice::from_raw_parts_mut(ptr, len));
}
"""

# Compile, register and load the Rust library on-the-fly!
compile_dylib("rust_upper", RUST_SRC)

@dylib_task("rust_upper")
def to_upper_rust(payload: str) -> str:
    pass

print(to_upper_rust("hello from rust").result())  # "HELLO FROM RUST"

Dive Deeper (Documentation Book)

Detailed documentation, guides, and implementation examples are available in our Documentation Book:

  • Asynchronous Event Loops: Non-blockingly await tasks using await handle.result_async() in FastAPI/asyncio. Read Chapter.
  • Batch Submissions: Submit multiple tasks under a single lock acquisition to avoid thread contention. Read Chapter.
  • Task Cancellation: Gracefully abort long-running background tasks mid-flight. Read Chapter.
  • Traceback Preservation: Capture stack traces on background worker threads and propagate them to the main thread. Read Chapter.
  • Memory Footprint & GC: Learn how Slab memory is reclaimed automatically using GC destructors. Read Chapter.

Performance At-a-Glance

We benchmarked Pyroxide against CPython's standard concurrency pools using identical compute payloads (recursive Fibonacci 20 workload) on Apple M1 Pro (8 cores, 16GB RAM):

Metric (500 Tasks) Pyroxide @dylib_task Pyroxide @task Threading (std) Multiprocessing
Execution Time 0.0200 s 0.3878 s 0.3742 s 2.0786 s
GIL Bypass ✅ Yes (GIL-Free) ❌ No ❌ No ✅ Yes
IPC / Serialization ✅ None (Shared Memory) ✅ None ✅ None ❌ High (pickle cost)
Relative Speedup 🔥 100x faster (18x faster than threads) 5x faster 5x faster Baseline (1x)
  • Bypassing the Multiprocessing Bottleneck: While Python's ProcessPoolExecutor takes over 2 seconds due to slow process spawning and heavy pickle IPC serialization, Pyroxide's @dylib_task runs native compiled plugins in just 20 milliseconds—offering a 100x speedup with zero-copy shared memory.

To run the comparative and basic benchmark suites locally:

# 1. Run detailed comparative benchmarks against CPython concurrency pools
python examples/benchmark_vs_alternatives.py

# 2. Run basic scheduling latency and asyncio benchmarks
python examples/benchmark.py

Contributing

Contributions are welcome! If you'd like to improve Pyroxide or add support for additional features, feel free to open an issue or submit a pull request on GitHub.

License

Pyroxide is licensed under any of:

at your option.

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

pyro3-0.3.3.tar.gz (62.4 kB view details)

Uploaded Source

Built Distributions

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

pyro3-0.3.3-cp38-abi3-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.8+Windows x86-64

pyro3-0.3.3-cp38-abi3-manylinux_2_34_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

pyro3-0.3.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

pyro3-0.3.3-cp38-abi3-macosx_11_0_arm64.whl (3.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

pyro3-0.3.3-cp38-abi3-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file pyro3-0.3.3.tar.gz.

File metadata

  • Download URL: pyro3-0.3.3.tar.gz
  • Upload date:
  • Size: 62.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for pyro3-0.3.3.tar.gz
Algorithm Hash digest
SHA256 c508d641cf2e88d53dadc7846b132052428f21e0a4c1235c4d4fcec69663f50a
MD5 2a004aeea4d318986886703aa2f0b6f3
BLAKE2b-256 7ef1f409c53d471fe6e8150bc5fb20ad6a40b5a5e966c4576619a8d4fcfffd33

See more details on using hashes here.

File details

Details for the file pyro3-0.3.3-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: pyro3-0.3.3-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.2 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for pyro3-0.3.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 66b8bb8ad499be7ea94988de32d2cab7afbe9d0939327a19f385153376da4f66
MD5 8bd269bd93a16a161c6c710ead2b556a
BLAKE2b-256 9154058cdd99e5e5d65cb561b3ee2c25ec2edc4362f73d881af7cf4259c01576

See more details on using hashes here.

File details

Details for the file pyro3-0.3.3-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyro3-0.3.3-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d01c878175a337f066c5ecbb2a6cec7bd2c01cf0eb8734b30c78ea28de70b5aa
MD5 f54f906b6933ccf3ff34ee6b45a01ab7
BLAKE2b-256 0e9157963205281a7643aba301cf653faf8c3b3bb6c2e655811bc9381652299c

See more details on using hashes here.

File details

Details for the file pyro3-0.3.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyro3-0.3.3-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 159bc3eacb3124d2e7f2e722a63f5326b056f7597fdccaec1689bd6f12348973
MD5 9a51d5c7d8a81638a96188fb98ea8467
BLAKE2b-256 bdb78b6999529385b1fcbdf8df1b8baac0ee24f7705dd51aabefe441265a82e0

See more details on using hashes here.

File details

Details for the file pyro3-0.3.3-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyro3-0.3.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ead30c3f4c5c3281c36f9bbcf3e5a7e2fa3ecd865a38a2ad408298ca68d46100
MD5 5cf2bb4a97538ca72f81f8248a8e08d5
BLAKE2b-256 828a089c7fe19c8505132cbff02556effcde81dd17bbaafdf2430712f793e333

See more details on using hashes here.

File details

Details for the file pyro3-0.3.3-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pyro3-0.3.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 116eead819c3dad5c51dc81a9e939980db9895bb726116a3b94a6f73cd1d00a2
MD5 453158eff7ebeda9317e52c27a515791
BLAKE2b-256 506b2f250b087c1f2ef8e5e62190fe1baf6dc2b20a9d44b197481e26ff7957cf

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