Skip to content

Commit

Permalink
Merge pull request #258 from neutrinoceros/fix_cpu_count
Browse files Browse the repository at this point in the history
BUG: fix CPU counting on Linux + Python <=3.12
  • Loading branch information
neutrinoceros authored Sep 14, 2024
2 parents ad314c4 + eb934da commit b8908b5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/inifix/_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import sys
from typing import Literal, NamedTuple, TextIO


Expand All @@ -17,4 +18,14 @@ class TaskResults(NamedTuple):
def get_cpu_count() -> int:
# this function exists primarily to be mocked
# instead of something we don't own
return os.cpu_count() or 1
base_cpu_count: int | None
if sys.version_info >= (3, 13):
base_cpu_count = os.process_cpu_count()
else:
if hasattr(os, "sched_getaffinity"):
# this function isn't available on all platforms
base_cpu_count = len(os.sched_getaffinity(0))
else:
# this proxy is good enough in most situations
base_cpu_count = os.cpu_count()
return base_cpu_count or 1

0 comments on commit b8908b5

Please sign in to comment.