Skip to main content

LOGO

Open In Colab


cthreads compiles a typed Python subset into native kernels: CPU via C++ (@Thread / @Threadable), optional GPU via Vulkan (@Gpu). You keep Python-shaped control flow (jobs, pools, sync, await) while the heavy work runs as OS-native / on-device compute in the background.

GPU support ships as cthreads-gpu (same import cthreads; do not install it alongside the CPU-only cthreads wheel). The type whitelist covers scalars, containers, and your own Threadable types.

Why cthreads

Hand-written C++/GLSL (or a full pybind project) is powerful but leaves the Python loop. threading stays on the GIL for CPU-bound bytecode; multiprocessing gets multi-core but pays process boundaries and pickling. cthreads sits in between: annotate -> compile -> run with the same job/pool/sync feel on CPU, and the same idea on GPU through cthreads-gpu.

It is a typed subset (not full Python). It is not a drop-in NumPy replacement. Prefer it when you want compiled native kernels without leaving a Python-shaped API.

cthreads SPH Python + NumPy SPH
realtime linear speedup up to 100x realtime on this scene
cthreads SPH simulation Python + NumPy SPH simulation

Fig. 1. Side-by-side Smoothed Particle Hydrodynamics (SPH) fluid demo under identical scene setup (same particle set, forces, timestep, and camera). Left: dynamics advanced with cthreads (@Thread kernels on a ThreadPool, shared native buffers). Right: the same solver expressed as Python + NumPy array kernels on the host. Overlay text at the top of each clip reports live run metrics; the second-to-last value is realtime speedup (simulated time per unit wall-clock). On this scene the NumPy path reaches up to about 100x realtime; the cthreads path is shown for visual parity of the fluid, not as a matched FPS bake-off in the GIF encode.


Interactive CPU Mandelbrot (pool + Shared + TBuffer): Open in Colab.


Docs


Install

Python >= 3.10, a C++17 compiler, and CMake >= 3.18 (CMake is only needed to build the native _ext module). Full toolchain notes: docs/install.md.

From PyPI

Published wheels (Linux / Windows x86_64) and the sdist are on PyPI:

pip install cthreads
# Vulkan GPU (@Gpu) support (full package; do not install alongside cthreads):
pip install cthreads-gpu

You still need a C++ compiler for the first thread(...) (user kernels). On Linux, wheels include a prebuilt _ext; CMake is only required if you install from the sdist or develop from source.

From this repo (editable)

python -m venv .venv
# activate, then:
pip install cmake ninja    # CMake/Ninja in the venv; compiler is still system/MSVC
pip install -e ".[test]"   # or: pip install -e .

First cthreads.thread(...) auto-runs cache-checked prepare + load_kernels. Call unload_kernels() before a force rebuild (thread(..., force=True) or prepare(force=True)).


Introduction

Annotate what should become a native kernel:

  • @Thread - functions / methods compiled to C++
  • @Threadable - classes compiled to C++ structs (shared state across kernels)
  • @Gpu - functions compiled to Vulkan compute (lists of scalars; see GPU guides)

Supported types

Allowed in annotations (arguments, returns, locals, Threadable fields):

  • int, float, bool, str
  • list[...] of allowed types
  • dict[...] of allowed types (typically dict[str, ...])
  • nested combinations of the above
  • @Threadable classes
  • any internal types imported by cthreads

This is a whitelist, not full Python. No arbitrary objects, no untyped values in kernels.

@Thread

Marks a function or method for compilation. Pass it to cthreads.thread(...) to run off the GIL.

from cthreads import Thread

@Thread
def my_example_function() -> None:
    return

Rules

  1. Typed parameters and a return type (use -> None when there is no value).
  2. No *args / **kwargs.
  3. Locals must be annotated with an allowed type (x: int = 0).
  4. Inside the body, only call other @Thread functions/methods, plus supported libraries (math, cthreads.math, cthreads.sync, cthreads.linalg) and builtins like len / range. No arbitrary Python.
  5. Return values must match the declared return type.
from cthreads import Thread

@Thread
def example_function(scale: float, values: list[float]) -> float:
    total: float = 0.0
    i: int = 0
    while i < len(values):
        total += scale * values[i]
        i += 1
    return total

@Threadable

Python's open object model does not map cleanly to C++. @Threadable marks a class so the compiler can emit a fixed C++ struct and marshal it safely.

from cthreads import Threadable

@Threadable
class MyExample:
    x: float
    y: float

Rules

  1. All fields are typed at class scope (dataclass-style annotations).
  2. Do not define / override __init__. The decorator injects a dataclass-style constructor (ExampleClass(1, "x") or ExampleClass(attr1=1); omitted fields zero / empty, matching C++ T{}).
  3. Kernel methods must use @Thread and take self like normal methods.
  4. Method argument / return annotations must be allowed types (or -> None).
from cthreads import Threadable, Thread

@Threadable
class ExampleClass:
    attr1: int
    attr2: str
    attr3: list[float]

    @Thread
    def method1(self) -> None:
        self.attr1 += 1

    @Thread
    def method2(self, string: str) -> str:
        return self.attr2 + string


obj = ExampleClass(0, "1", [2.0, 3.0])

obj.method1()
print(obj.attr1, obj.method2(" 1"))  # 1 1 1

Why Threadables?

  • shared state for worker threads
  • typed containers / domain objects
  • grouping related kernel methods

Run a @Thread

import asyncio
import cthreads
from cthreads import Thread

@Thread
def example_function(lhs: float, rhs: float, count: int) -> float:
    for i in range(count):
        lhs += rhs
    return lhs

# Sync: Job -> join -> result
job = cthreads.thread(example_function, 1.5, 2.0, 200)
job.join()  # starts if needed; blocks this thread (GIL released in C++)
result = job.result()

# Async: await auto-starts and returns the result (event loop stays free)
async def main() -> None:
    job = cthreads.thread(example_function, 1.5, 2.0, 200)
    result = await job
    print(result)

asyncio.run(main())

Signature: cthreads.thread(fn, *args, force: bool = False, **kwargs) -> Job.


GPU (@Gpu, from 0.2.0)

Vulkan compute kernels use the same "annotate then launch" idea on a separate backend:

from cthreads.gpu import Gpu, GlobalIdx, gpu

@Gpu
def saxpy(n: int, a: float, x: list[float], y: list[float]) -> None:
    i: int = GlobalIdx.x
    if i >= n:
        return # do not return None. Simply return
    y[i] = a * x[i] + y[i]

x = [1.0, 2.0, 3.0, 4.0]
y = [10.0, 20.0, 30.0, 40.0]
gpu(saxpy, len(x), 2.0, x, y).join()

Full guides (concepts, best practices, examples): docs/guide/gpu/README.md. Install / drivers: docs/install.md.


Metadata

Release files for cthreads-gpu 0.2.2

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

Built distributions (wheels)

Table of built distributions (wheels) for cthreads-gpu 0.2.2
File Actions
cthreads_gpu-0.2.2-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for cthreads_gpu-0.2.2-cp313-cp313-win_amd64.whl
cthreads_gpu-0.2.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details for cthreads_gpu-0.2.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
cthreads_gpu-0.2.2-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for cthreads_gpu-0.2.2-cp312-cp312-win_amd64.whl
cthreads_gpu-0.2.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details for cthreads_gpu-0.2.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
cthreads_gpu-0.2.2-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for cthreads_gpu-0.2.2-cp311-cp311-win_amd64.whl
cthreads_gpu-0.2.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details for cthreads_gpu-0.2.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
cthreads_gpu-0.2.2-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for cthreads_gpu-0.2.2-cp310-cp310-win_amd64.whl
cthreads_gpu-0.2.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details for cthreads_gpu-0.2.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Total release size: 28.5 MB

Release files / cthreads_gpu-0.2.2-cp313-cp313-win_amd64.whl

Download URL cthreads_gpu-0.2.2-cp313-cp313-win_amd64.whl
Size 4.2 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
2e8b12354bc719b069e83a61f33d07e5d368f2f312113bfefae38001d5dd6da8
BLAKE2b-256 checksum
How to use checksums
07eea18a48f58b3375f4a713d9231ed8282849fbee85c33a2c279dc45d9417bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL cthreads_gpu-0.2.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 6.9 MB
Tags CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
567b0afd290ef51a8ac4ec18927785e5d60ee39a4af9e2a998006091ba307d0a
BLAKE2b-256 checksum
How to use checksums
4aa09b5b3a751bf54b39f14d62117a2af2acc29f97e5234edf7f1a32a0248f8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp312-cp312-win_amd64.whl

Download URL cthreads_gpu-0.2.2-cp312-cp312-win_amd64.whl
Size 3.2 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
5afc7878e5d46cf32d639856713ddf92cb14b59cda6204182b48566869be3116
BLAKE2b-256 checksum
How to use checksums
69b54dd41a235313d644f9af7831da6d24558e109f1c841386567a5ff922f116
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL cthreads_gpu-0.2.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 5.2 MB
Tags CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
37b24a4cc2423c5ef883000ea99190c5f6bee2254fcbe47613c425ddc0e51b9c
BLAKE2b-256 checksum
How to use checksums
cfa26074e61a4e605119eb33e4b9e39b438da18c29068d01daab193704a1a51c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp311-cp311-win_amd64.whl

Download URL cthreads_gpu-0.2.2-cp311-cp311-win_amd64.whl
Size 2.3 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
89e182f329db0620ad5bae53344e954ef5589900c82ffbc0f71595aa424c3679
BLAKE2b-256 checksum
How to use checksums
5dbcc3cacc7469f0dcca7c6273aeedb44db12b42b31af01817249bedf4827923
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL cthreads_gpu-0.2.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 3.5 MB
Tags CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
938648530af4ac146780ee0308229fa1c55f022019b165e96820059cf6ada886
BLAKE2b-256 checksum
How to use checksums
903122d28f6d0569139ca750b794a414fd7fb7b52268a600743074d27f2791bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp310-cp310-win_amd64.whl

Download URL cthreads_gpu-0.2.2-cp310-cp310-win_amd64.whl
Size 1.3 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
ab19926cc168f77dff69cf253ebb6f3e3fc3bf84938b47a81b946fe11de0607f
BLAKE2b-256 checksum
How to use checksums
2b5292f1c5cc20f9e082fc7d7c6fe43ecf409ee6f762edb16fea40fc73b60902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / cthreads_gpu-0.2.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL cthreads_gpu-0.2.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 1.9 MB
Tags CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
199e71fa3e30e05ce92cb227d4f82f0a0ae9f03196437361de79e95ea5680878
BLAKE2b-256 checksum
How to use checksums
ebc5b5349b33fc06796bc73bfa2de59b1ef0f9cdd488cc7f8549e663d3c52028
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for cthreads-gpu 0.2.2

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.

Full provenance

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for cthreads_gpu-0.2.2-cp313-cp313-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

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