2

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()
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
  • this suggests it is not possible: https://stackoverflow.com/questions/42658331/python-3-on-macos-how-to-set-process-affinity – jkr Feb 13 '23 at 20:31

0 Answers0