d3ro-voice/apps/desktop/sidecar/cpu_budget.py

19 lines
651 B
Python

"""Bound local inference workers so foreground input has scheduling headroom.
Keep the policy aligned with src/main/utils/inference-cpu-budget.ts.
This limits inference workers, not process affinity or total process CPU usage.
"""
import os
def inference_cpu_threads(available_cpus: int | None) -> int:
if available_cpus is None or available_cpus < 1:
return 1
return max(1, min(4, available_cpus - 2))
def local_inference_cpu_threads() -> int:
# Python 3.13+ exposes the CPUs available to this process, including affinity.
count = getattr(os, "process_cpu_count", os.cpu_count)()
return inference_cpu_threads(count)