Get the available CPU count for this system. Takes the minimum value from the following locations: - Total system cpus available on the host. - CPU Affinity (if set) - Cgroups limit (if set)
()
| 42 | |
| 43 | |
| 44 | def cpu_count(): |
| 45 | """Get the available CPU count for this system. |
| 46 | |
| 47 | Takes the minimum value from the following locations: |
| 48 | |
| 49 | - Total system cpus available on the host. |
| 50 | - CPU Affinity (if set) |
| 51 | - Cgroups limit (if set) |
| 52 | """ |
| 53 | count = os.cpu_count() |
| 54 | |
| 55 | # Check CPU affinity if available |
| 56 | if psutil is not None: |
| 57 | try: |
| 58 | affinity_count = len(psutil.Process().cpu_affinity()) |
| 59 | if affinity_count > 0: |
| 60 | count = min(count, affinity_count) |
| 61 | except Exception: |
| 62 | pass |
| 63 | |
| 64 | # Check cgroups if available |
| 65 | if sys.platform == "linux": |
| 66 | quota, period = _try_extract_cgroup_cpu_quota() |
| 67 | if quota is not None and period is not None: |
| 68 | # We round up on fractional CPUs |
| 69 | cgroups_count = math.ceil(quota / period) |
| 70 | if cgroups_count > 0: |
| 71 | count = min(count, cgroups_count) |
| 72 | |
| 73 | return count |
| 74 | |
| 75 | |
| 76 | CPU_COUNT = cpu_count() |