pytest-dev / pytest-dev/pytest-xdist
Improve autodetection of number of (available CPUs)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 287
- Avg merge
- 9h 30m
- Merged PRs (30d)
- 2
Description
The current implementation of -n auto is not working as hoped on high-performance computing environments where processes are assigned a number of cores they can use. For example, when running a PyTest job with 9 cores requested in the job submission on a compute node with 36 cores, pytest-xdist runs 36 processes. Ideally, it should use 9.
This is related to the logic in def pytest_xdist_auto_num_workers(...) in src/xdist/plugin.py. This function first tries the psutil package, and only then falls back to os.sched_getaffinity, which gives the correct number. If one has accidentally psutil installed (difficult to avoid), the autodetection does not produce the best answer.
For your information, in the scenario sketched above, these are the results of various functions to get the number of CPU cores:
>>> len(os.sched_getaffinity(0))
9
>>> len(psutil.Process().cpu_affinity())
9
>>> psutil.cpu_count(logical=True)
36
>>> psutil.cpu_count(logical=False)
36
>>> os.cpu_count()
36
>>> multiprocessing.cpu_count()
36
The function os.sched_getaffinity was introduced in Python 3.3, older than the oldest supported version by pytest-xdist. As far as I understand, this function is not available in all environments. (Unclear to me, I cannot test on other OSes.) According to documentation, len(psutil.Process().cpu_affinity()) should at least work on Linux and Windows. There may still be a need to fall back to other functions. Trying them in the order listed above seems reasonable.
This suggestion may interfere with the option config.option.numprocesses. In compute environments, the option is not so relevant because the number of cores is managed by the queueing system. (Also, hyperthreading is often disabled in such scenarios because it degrades raw compute performance. It mainly helps for io-bound workloads.)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/xdist/plugin.py and the pytest_xdist_auto_num_workers(...) entry point, then compare the current psutil and os.sched_getaffinity fallback order. Done means -n auto uses the CPU count assigned by the compute environment when available while retaining a fallback for environments without those APIs; verify the relevant autodetection behavior across supported environments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100