Beispiel #1
0
def test_only_physical_cores_with_user_limitation():
    # Check that user limitation for the available number of cores is
    # respected even if only_physical_cores == True
    cpu_count_mp = mp.cpu_count()
    cpu_count_user = _cpu_count_user(cpu_count_mp)

    if cpu_count_user < cpu_count_mp:
        assert cpu_count() == cpu_count_user
        assert cpu_count(only_physical_cores=True) == cpu_count_user
Beispiel #2
0
def test_only_physical_cores_error():
    # Check the warning issued by cpu_count(only_physical_cores=True) when
    # unable to retrieve the number of physical cores.
    if sys.platform != "linux":
        pytest.skip()

    # if number of available cpus is already restricted, cpu_count will return
    # that value and no warning is issued even if only_physical_cores == True.
    # (tested in another test: test_only_physical_cores_with_user_limitation
    cpu_count_mp = mp.cpu_count()
    if _cpu_count_user(cpu_count_mp) < cpu_count_mp:
        pytest.skip()

    start_dir = os.path.abspath('.')

    with tempfile.TemporaryDirectory() as tmp_dir:
        # Write bad lscpu program
        lscpu_path = tmp_dir + '/lscpu'
        with open(lscpu_path, 'w') as f:
            f.write("#!/bin/sh\n" "exit(1)")
        os.chmod(lscpu_path, 0o777)

        try:
            old_path = os.environ['PATH']
            os.environ['PATH'] = tmp_dir + ":" + old_path

            # clear the cache otherwise the warning is not triggered
            import loky.backend.context
            loky.backend.context.physical_cores_cache = None

            with pytest.warns(UserWarning,
                              match="Could not find the number of"
                              " physical cores"):
                cpu_count(only_physical_cores=True)

            # Should not warn the second time
            with pytest.warns(None) as record:
                cpu_count(only_physical_cores=True)
                assert not record

        finally:
            os.environ['PATH'] = old_path