Determine number of physical cores that current process can use (best effort).
()
| 974 | |
| 975 | |
| 976 | def get_available_threads() -> int: |
| 977 | """Determine number of physical cores that current process can use (best effort).""" |
| 978 | global _AVAILABLE_THREADS |
| 979 | if _AVAILABLE_THREADS is not None: |
| 980 | return _AVAILABLE_THREADS |
| 981 | |
| 982 | # This takes into account -X cpu_count and/or PYTHON_CPU_COUNT, but always |
| 983 | # counts virtual cores (which is not what we want for CPU bound tasks). |
| 984 | os_cpu_count = os.cpu_count() |
| 985 | if PSUTIL_AVAILABLE: |
| 986 | # Unlike os, psutil can determine number of physical cores. |
| 987 | psutil_cpu_count = psutil.cpu_count(logical=False) |
| 988 | else: |
| 989 | psutil_cpu_count = None |
| 990 | |
| 991 | if psutil_cpu_count and os_cpu_count: |
| 992 | cpu_count = min(psutil_cpu_count, os_cpu_count) |
| 993 | elif psutil_cpu_count or os_cpu_count: |
| 994 | cpu_count = psutil_cpu_count or os_cpu_count |
| 995 | else: |
| 996 | # A conservative fallback in case we cannot determine CPU count in any way. |
| 997 | cpu_count = 4 |
| 998 | |
| 999 | affinity: set[int] | list[int] | None = None |
| 1000 | # Not available on old Python versions on some platforms. |
| 1001 | if sys.platform == "linux": |
| 1002 | affinity = os.sched_getaffinity(0) |
| 1003 | if PSUTIL_AVAILABLE and sys.platform != "darwin": |
| 1004 | # Currently not supported on macOS. |
| 1005 | affinity = psutil.Process().cpu_affinity() |
| 1006 | |
| 1007 | assert cpu_count is not None |
| 1008 | if affinity: |
| 1009 | available_threads = min(cpu_count, len(affinity)) |
| 1010 | else: |
| 1011 | available_threads = cpu_count |
| 1012 | _AVAILABLE_THREADS = available_threads |
| 1013 | return available_threads |
| 1014 | |
| 1015 | |
| 1016 | def hash_path_stem(s: str) -> int: |
no test coverage detected
searching dependent graphs…