Python's os.cpu_count()
suggests using len(os.sched_getaffinity(0))
. However, this is not available on my Mac:
>>> len(os.sched_getaffinity(0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'sched_getaffinity'
From Interface to the scheduler:
They are only available on some Unix platforms.
Is there a good alternative (preference to built-in solutions) to os.sched_getaffinity
for macOS?
Here is my short-term workaround:
import multiprocessing
import os
def _get_core_count() -> int:
try:
# NOTE: only available on some Unix platforms
return len(os.sched_getaffinity(0)) # type: ignore[attr-defined]
except AttributeError:
return multiprocessing.cpu_count()