Skip to content

Commit

Permalink
Use setproctitle if available to show state (#696)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
eqvinox and nicoddemus committed Sep 3, 2021
1 parent 305aeea commit 766e67c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Expand Up @@ -17,6 +17,7 @@ jobs:
- "py39-pytestlatest"
- "py38-pytestmain"
- "py38-psutil"
- "py38-setproctitle"

os: [ubuntu-latest, windows-latest]
include:
Expand All @@ -32,6 +33,8 @@ jobs:
python: "3.8"
- tox_env: "py38-psutil"
python: "3.8"
- tox_env: "py38-setproctitle"
python: "3.8"

steps:
- uses: actions/checkout@v1
Expand Down
20 changes: 20 additions & 0 deletions README.rst
Expand Up @@ -317,6 +317,26 @@ Since version 2.0, the following functions are also available in the ``xdist`` m
"""
Identifying workers from the system environment
-----------------------------------------------

*New in version UNRELEASED TBD FIXME*

If the `setproctitle`_ package is installed, ``pytest-xdist`` will use it to
update the process title (command line) on its workers to show their current
state. The titles used are ``[pytest-xdist running] file.py/node::id`` and
``[pytest-xdist idle]``, visible in standard tools like ``ps`` and ``top`` on
Linux, Mac OS X and BSD systems. For Windows, please follow `setproctitle`_'s
pointer regarding the Process Explorer tool.

This is intended purely as an UX enhancement, e.g. to track down issues with
long-running or CPU intensive tests. Errors in changing the title are ignored
silently. Please try not to rely on the title format or title changes in
external scripts.

.. _`setproctitle`: https://pypi.org/project/setproctitle/


Uniquely identifying the current test run
-----------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions changelog/696.feature.rst
@@ -0,0 +1,3 @@
On Linux, the process title now changes to indicate the current worker state (running/idle).

Depends on the `setproctitle <https://pypi.org/project/setproctitle/>`__ package, which can be installed with ``pip install pytest-xdist[setproctitle]``.
6 changes: 5 additions & 1 deletion setup.py
Expand Up @@ -18,7 +18,11 @@
platforms=["linux", "osx", "win32"],
packages=find_packages(where="src"),
package_dir={"": "src"},
extras_require={"testing": ["filelock"], "psutil": ["psutil>=3.0"]},
extras_require={
"testing": ["filelock"],
"psutil": ["psutil>=3.0"],
"setproctitle": ["setproctitle"],
},
entry_points={
"pytest11": ["xdist = xdist.plugin", "xdist.looponfail = xdist.looponfail"]
},
Expand Down
20 changes: 20 additions & 0 deletions src/xdist/remote.py
Expand Up @@ -16,6 +16,21 @@

from _pytest.config import _prepareconfig, Config

try:
from setproctitle import setproctitle
except ImportError:

def setproctitle(title):
pass


def worker_title(title):
try:
setproctitle(title)
except Exception:
# changing the process name is very optional, no errors please
pass


class WorkerInteractor:
def __init__(self, config, channel):
Expand Down Expand Up @@ -85,9 +100,14 @@ def run_one_test(self, torun):
else:
nextitem = None

worker_title("[pytest-xdist running] %s" % item.nodeid)

start = time.time()
self.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
duration = time.time() - start

worker_title("[pytest-xdist idle]")

self.sendevent(
"runtest_protocol_complete", item_index=self.item_index, duration=duration
)
Expand Down
9 changes: 9 additions & 0 deletions tox.ini
Expand Up @@ -4,6 +4,7 @@ envlist=
py{36,37,38,39}-pytestlatest
py38-pytestmain
py38-psutil
py38-setproctitle

[testenv]
extras = testing
Expand All @@ -21,6 +22,14 @@ deps = pytest
commands =
pytest {posargs:-k psutil}

[testenv:py38-setproctitle]
extras =
testing
setproctitle
deps = pytest
commands =
pytest {posargs}

[testenv:release]
changedir=
decription = do a release, required posarg of the version number
Expand Down

0 comments on commit 766e67c

Please sign in to comment.