Path 1: 1 calls (0.5)

None (1)

1def _query_cpu() -> int | None:
2    """Try to determine number of CPUs allotted in a docker container.
3
4    This is based on discussion and copied from suggestions in
5    https://bugs.python.org/issue36054.
6    """
7    cpu_quota, avail_cpu = None, None
8
9    if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
10        with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
11            # Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
12            cpu_quota = int(file.read().rstrip())
13
14    if (
15        cpu_quota
16        and cpu_quota != -1
17        and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
18    ):
19        with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
20            cpu_period = int(file.read().rstrip())
21        # Divide quota by period and you should get num of allotted CPU to the container, rounded down if fractional.
22        avail_cpu = int(cpu_quota / cpu_period)
23    elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
24        with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
25            cpu_shares = int(file.read().rstrip())
26        # For AWS, gives correct value * 1024.
27        avail_cpu = int(cpu_shares / 1024)
28
29    # In K8s Pods also a fraction of a single core could be available
30    # As multiprocessing is not able to run only a "fraction" of process
31    # assume we have 1 CPU available
32    if avail_cpu == 0:
33        avail_cpu = 1
34
35    return avail_cpu
            

Path 2: 1 calls (0.5)

1 (1)

1def _query_cpu() -> int | None:
2    """Try to determine number of CPUs allotted in a docker container.
3
4    This is based on discussion and copied from suggestions in
5    https://bugs.python.org/issue36054.
6    """
7    cpu_quota, avail_cpu = None, None
8
9    if Path("/sys/fs/cgroup/cpu/cpu.cfs_quota_us").is_file():
10        with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", encoding="utf-8") as file:
11            # Not useful for AWS Batch based jobs as result is -1, but works on local linux systems
12            cpu_quota = int(file.read().rstrip())
13
14    if (
15        cpu_quota
16        and cpu_quota != -1
17        and Path("/sys/fs/cgroup/cpu/cpu.cfs_period_us").is_file()
18    ):
19        with open("/sys/fs/cgroup/cpu/cpu.cfs_period_us", encoding="utf-8") as file:
20            cpu_period = int(file.read().rstrip())
21        # Divide quota by period and you should get num of allotted CPU to the container, rounded down if fractional.
22        avail_cpu = int(cpu_quota / cpu_period)
23    elif Path("/sys/fs/cgroup/cpu/cpu.shares").is_file():
24        with open("/sys/fs/cgroup/cpu/cpu.shares", encoding="utf-8") as file:
25            cpu_shares = int(file.read().rstrip())
26        # For AWS, gives correct value * 1024.
27        avail_cpu = int(cpu_shares / 1024)
28
29    # In K8s Pods also a fraction of a single core could be available
30    # As multiprocessing is not able to run only a "fraction" of process
31    # assume we have 1 CPU available
32    if avail_cpu == 0:
33        avail_cpu = 1
34
35    return avail_cpu