Persian: fa/docs/17-ecosystem-python.md
Catalog: catalogs/ECOSYSTEM_PYTHON.md
CPython is excellent for orchestration (FastAPI, Django, data glue). It is often not the right place for tight CPU loops, bulk parsing, or alloc-heavy codecs. Export those kernels as a stable C ABI shared library and call them from Python.
| Approach | Use when |
|---|---|
ctypes (shipped: bindings/python) |
Simple C functions; handbook default |
| cffi | Richer declarations; still C ABI |
| Extension module (C API) | Need Python objects in-process; more coupling |
| NumPy / buffer protocol | Arrays: pass data pointer + nbytes; document who owns memory |
See 09 FFI ABI: free Zig allocations only with the matching zbr_free (or use caller-owned buffers).
Client → FastAPI / Django / Flask (auth, ORM, validation)
│
▼ ctypes / cffi / TurboAPI native_route
Zig shared library (ReleaseFast hot path)
| Pattern | Description |
|---|---|
| Sidecar FFI | Keep uvicorn/gunicorn; call zbr_* on hot endpoints |
| Zig HTTP core | Upstream TurboAPI: FastAPI-compatible API with Zig server — cite upstream; do not treat their RPS as ours |
| Batch worker | Celery/RQ worker loads the same .so / .dll |
- zignal / PyPI
zignal-processing— image processing in Zig with Python bindings (arrufat/zignal) - TurboAPI — Zig HTTP behind a FastAPI-like surface (justrach/turboAPI)
- Profile (cProfile, py-spy, scalene) — move only measured hot spots.
- Minimize crossings — batch work; avoid per-element FFI calls.
- Zero-copy —
memoryview/ NumPy contiguous buffers; length + pointer. - Build — narrate
ReleaseFastfor speed claims; Debug for tests. - Prove locally —
.\scripts\microbench-zbr.ps1and binding tests; never paste TFB/TurboAPI numbers as handbook results.
Anti-patterns: rewriting the ORM in Zig; claiming ASGI framework replacement without measurement; mixing CRT free paths on Windows.
| Artifact | Path |
|---|---|
| Typed ctypes wrapper | bindings/python |
| Tests | pytest via verify scripts |
| Guide | guides/zig-behind-your-api.md |
| Item | Status |
|---|---|
| ctypes + QA gates | Shipped |
| FastAPI sidecar sketch | Shipped: examples/domains/python-fastapi-sidecar |
| Vendoring TurboAPI | Out of scope |