Return the number of CPUs this process may run on. Honors the scheduler affinity mask (``taskset``, cpusets, ``docker run --cpuset-cpus``) via :func:`os.process_cpu_count` on Python 3.13+ or :func:`os.sched_getaffinity` on older Linux. Falls back to :func:`os.cpu_count` (or 2 if und
()
| 35 | |
| 36 | |
| 37 | def available_cpu_count() -> int: |
| 38 | """Return the number of CPUs this process may run on. |
| 39 | |
| 40 | Honors the scheduler affinity mask (``taskset``, cpusets, |
| 41 | ``docker run --cpuset-cpus``) via :func:`os.process_cpu_count` on |
| 42 | Python 3.13+ or :func:`os.sched_getaffinity` on older Linux. Falls |
| 43 | back to :func:`os.cpu_count` (or 2 if undetermined) where neither |
| 44 | is available. |
| 45 | """ |
| 46 | count = None |
| 47 | if hasattr(os, 'process_cpu_count'): |
| 48 | count = os.process_cpu_count() |
| 49 | elif hasattr(os, 'sched_getaffinity'): |
| 50 | try: |
| 51 | count = len(os.sched_getaffinity(0)) |
| 52 | except OSError: |
| 53 | pass |
| 54 | return count or os.cpu_count() or 2 |
| 55 | |
| 56 | |
| 57 | def _read_text(path: str) -> str | None: |
no outgoing calls