Coverage for src / lilbee / runtime / cpu.py: 100%
16 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-05-15 20:55 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-05-15 20:55 +0000
1"""CPU concurrency policy for compute-bound parallelism.
3Use ``cpu_quota()`` to bound thread pools and asyncio semaphores that
4schedule CPU-heavy work (chat inference, PDF rasterization, embedding,
5tokenization). Capping at half of cpu_count keeps the asyncio main
6thread scheduler share so the TUI stays responsive while a worker
7storm is in flight.
9Do NOT use this for HTTP request concurrency: that lives under
10``cfg.crawl_max_concurrent`` and is governed by remote-side rate
11limits, not local CPU.
12"""
14from __future__ import annotations
16import logging
17import os
19log = logging.getLogger(__name__)
21_ENV_VAR = "LILBEE_CPU_QUOTA"
24def cpu_quota() -> int:
25 """Return the CPU concurrency cap; honors ``LILBEE_CPU_QUOTA`` override.
27 Default is ``max(1, os.cpu_count() // 2)``. The override accepts a
28 positive integer; non-positive or unparseable values fall back to
29 the default and a warning is logged once per call.
30 """
31 override = os.environ.get(_ENV_VAR)
32 if override is not None:
33 try:
34 value = int(override)
35 if value > 0:
36 return value
37 except ValueError:
38 pass # bad override falls through to the warning + default below
39 log.warning(
40 "Ignoring %s=%r: must be a positive integer; using default.",
41 _ENV_VAR,
42 override,
43 )
44 return max(1, (os.cpu_count() or 2) // 2)