Background
Python 3.13 introduced new functionality to address the case "how many (logical) CPU cores can this Python program use" via the os.process_cpu_count function. In particular, it also introduced the related -X cpu_count option and PYTHON_CPU_COUNT environment variable, which can be used to override the auto-detected value, thus serving a similar purpose to the PYTEST_XDIST_AUTO_NUM_WORKERS environment variable. These two override mechanisms were retrofitted to the os.cpu_count function as well (the variant that does not take CPU affinity into account).
The issue
pytest-xdist does not explicitly respect these standard Python override mechanisms in its pytest_xdist_auto_num_workers hook. Assuming that PYTEST_XDIST_AUTO_NUM_WORKERS is unset:
- If
psutil is available, then pytest-xdist will dismiss -X cpu_count/PYTHON_CPU_COUNT, because psutil uses C code to calculate the true logical process count available to this Python process.1
- Otherwise,
os.sched_getaffinity will be queried, which also dismisses -X cpu_count/PYTHON_CPU_COUNT.
The question
Should these override mechanisms be respected?
- I would argue yes, because they clearly state intent (beyond just the pytest-xdist interaction) for Python to
lie claim to the script that only so-and-so many CPU cores are available.
- However, the code was written with pre-3.13 semantics in mind, and still runs on pre-3.13 systems, so retrofitting these overrides there is a compatibility break, and should perhaps not be taken lightly.
- Or perhaps the correct choice here is to go version-specific, i.e., to only respect the overrides on 3.13 and higher.
Going forward
If the override mechanisms are to be respected (whether in general, or on 3.13 and higher only), I would be willing to write a PR.
Background
Python 3.13 introduced new functionality to address the case "how many (logical) CPU cores can this Python program use" via the
os.process_cpu_countfunction. In particular, it also introduced the related-X cpu_countoption andPYTHON_CPU_COUNTenvironment variable, which can be used to override the auto-detected value, thus serving a similar purpose to thePYTEST_XDIST_AUTO_NUM_WORKERSenvironment variable. These two override mechanisms were retrofitted to theos.cpu_countfunction as well (the variant that does not take CPU affinity into account).The issue
pytest-xdist does not explicitly respect these standard Python override mechanisms in its
pytest_xdist_auto_num_workershook. Assuming thatPYTEST_XDIST_AUTO_NUM_WORKERSis unset:psutilis available, then pytest-xdist will dismiss-X cpu_count/PYTHON_CPU_COUNT, becausepsutiluses C code to calculate the true logical process count available to this Python process.1os.sched_getaffinitywill be queried, which also dismisses-X cpu_count/PYTHON_CPU_COUNT.The question
Should these override mechanisms be respected?
lieclaim to the script that only so-and-so many CPU cores are available.Going forward
If the override mechanisms are to be respected (whether in general, or on 3.13 and higher only), I would be willing to write a PR.
Footnotes
It makes sense that
psutil– which aims to query and report true system properties – would not respect these Python-specific overrides. ↩