Use os.sched_getaffinity for cpu counts when available

This commit is contained in:
Joe Bateson 2023-08-28 19:20:23 -07:00 committed by Anthony Sottile
parent 9ebda91889
commit ea8244b229
2 changed files with 33 additions and 2 deletions

View file

@ -24,6 +24,14 @@ TRet = TypeVar('TRet')
def cpu_count() -> int:
try:
# On systems that support it, this will return a more accurate count of
# usable CPUs for the current process, which will take into account
# cgroup limits
return len(os.sched_getaffinity(0))
except AttributeError:
pass
try:
return multiprocessing.cpu_count()
except NotImplementedError: