MCPcopy Create free account
hub / github.com/dask/dask / cpu_count

Function cpu_count

dask/system.py:44–73  ·  view source on GitHub ↗

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)

()

Source from the content-addressed store, hash-verified

42
43
44def 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
76CPU_COUNT = cpu_count()

Callers 4

test_cpu_countFunction · 0.90
test_cpu_count_cgroupsFunction · 0.90
system.pyFile · 0.85

Calls 3

minFunction · 0.85
cpu_affinityMethod · 0.80

Tested by 3

test_cpu_countFunction · 0.72
test_cpu_count_cgroupsFunction · 0.72