Module

_cpu_affinity

CPU affinity for worker threads (Linux only).

When enabled, pins each worker to a dedicated CPU core to reduce cache thrashing and improve throughput on multi-core systems.

Functions

maybe_pin_worker 2 None
Pin the current thread/process to a CPU core when enabled on Linux. Called at …
def maybe_pin_worker(worker_id: int, config: ServerConfig) -> None

Pin the current thread/process to a CPU core when enabled on Linux.

Called at worker startup. No-op when:

  • cpu_affinity is False
  • Not running on Linux (os.sched_setaffinity is Linux-only)
  • sched_setaffinity raises (e.g. restricted cpuset in containers)
Parameters
Name Type Description
worker_id int

Worker index (0, 1, ...).

config ServerConfig

Server configuration (reads cpu_affinity).