Skip to content

Commit 766e67c

Browse files
eqvinoxnicoddemus
andauthored
Use setproctitle if available to show state (#696)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 305aeea commit 766e67c

File tree

6 files changed

+60
-1
lines changed

6 files changed

+60
-1
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- "py39-pytestlatest"
1818
- "py38-pytestmain"
1919
- "py38-psutil"
20+
- "py38-setproctitle"
2021

2122
os: [ubuntu-latest, windows-latest]
2223
include:
@@ -32,6 +33,8 @@ jobs:
3233
python: "3.8"
3334
- tox_env: "py38-psutil"
3435
python: "3.8"
36+
- tox_env: "py38-setproctitle"
37+
python: "3.8"
3538

3639
steps:
3740
- uses: actions/checkout@v1

README.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,26 @@ Since version 2.0, the following functions are also available in the ``xdist`` m
317317
"""
318318
319319
320+
Identifying workers from the system environment
321+
-----------------------------------------------
322+
323+
*New in version UNRELEASED TBD FIXME*
324+
325+
If the `setproctitle`_ package is installed, ``pytest-xdist`` will use it to
326+
update the process title (command line) on its workers to show their current
327+
state. The titles used are ``[pytest-xdist running] file.py/node::id`` and
328+
``[pytest-xdist idle]``, visible in standard tools like ``ps`` and ``top`` on
329+
Linux, Mac OS X and BSD systems. For Windows, please follow `setproctitle`_'s
330+
pointer regarding the Process Explorer tool.
331+
332+
This is intended purely as an UX enhancement, e.g. to track down issues with
333+
long-running or CPU intensive tests. Errors in changing the title are ignored
334+
silently. Please try not to rely on the title format or title changes in
335+
external scripts.
336+
337+
.. _`setproctitle`: https://pypi.org/project/setproctitle/
338+
339+
320340
Uniquely identifying the current test run
321341
-----------------------------------------
322342

changelog/696.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
On Linux, the process title now changes to indicate the current worker state (running/idle).
2+
3+
Depends on the `setproctitle <https://pypi.org/project/setproctitle/>`__ package, which can be installed with ``pip install pytest-xdist[setproctitle]``.

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
platforms=["linux", "osx", "win32"],
1919
packages=find_packages(where="src"),
2020
package_dir={"": "src"},
21-
extras_require={"testing": ["filelock"], "psutil": ["psutil>=3.0"]},
21+
extras_require={
22+
"testing": ["filelock"],
23+
"psutil": ["psutil>=3.0"],
24+
"setproctitle": ["setproctitle"],
25+
},
2226
entry_points={
2327
"pytest11": ["xdist = xdist.plugin", "xdist.looponfail = xdist.looponfail"]
2428
},

src/xdist/remote.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
from _pytest.config import _prepareconfig, Config
1818

19+
try:
20+
from setproctitle import setproctitle
21+
except ImportError:
22+
23+
def setproctitle(title):
24+
pass
25+
26+
27+
def worker_title(title):
28+
try:
29+
setproctitle(title)
30+
except Exception:
31+
# changing the process name is very optional, no errors please
32+
pass
33+
1934

2035
class WorkerInteractor:
2136
def __init__(self, config, channel):
@@ -85,9 +100,14 @@ def run_one_test(self, torun):
85100
else:
86101
nextitem = None
87102

103+
worker_title("[pytest-xdist running] %s" % item.nodeid)
104+
88105
start = time.time()
89106
self.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
90107
duration = time.time() - start
108+
109+
worker_title("[pytest-xdist idle]")
110+
91111
self.sendevent(
92112
"runtest_protocol_complete", item_index=self.item_index, duration=duration
93113
)

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ envlist=
44
py{36,37,38,39}-pytestlatest
55
py38-pytestmain
66
py38-psutil
7+
py38-setproctitle
78

89
[testenv]
910
extras = testing
@@ -21,6 +22,14 @@ deps = pytest
2122
commands =
2223
pytest {posargs:-k psutil}
2324

25+
[testenv:py38-setproctitle]
26+
extras =
27+
testing
28+
setproctitle
29+
deps = pytest
30+
commands =
31+
pytest {posargs}
32+
2433
[testenv:release]
2534
changedir=
2635
decription = do a release, required posarg of the version number

0 commit comments

Comments
 (0)