Post #2065492
2026-05-05 18:54 UTC
On Python, `loky.cpu_count()` will take cgroups settings, common in Docker and Kubernetes, into account when calculating number of available cores.
Are there any other Python APIs that support cgroups options? Standard library doesn't.
(On Rust you can use `num_cpus` crate, on R there's parallely::availableCores()).
#python
Replies (3)
-
@jake@mastodon.theorangeone.net 2026-05-05 19:58
@itamarst I thought the point of `os.process_cpu_count()` was that it's aware of external limits on the process? https://docs.python.org/3/library/os.html#os.process_cpu_count
-
@pauricthelodger@mastodon.social 2026-05-05 21:26
@itamarst Does psutil handle it these days? https://psutil.readthedocs.io/stable/#psutil.cpu_count
-
@encukou@mastodon.social 2026-05-06 06:41
@itamarst File a bug for stdlib? Meanwhile you can use the cgroups2 CPU bandwidth API: read two numbers from `/sys/fs/cgroup/cpu.max`, divide, round up. https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html#:~:text=cpu.max