summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-02-26 15:56:50 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2019-02-26 15:56:50 +0100
commit31324467736c9a0fc63f47f11c6810dee23f05e2 (patch)
tree3d5d63c7e042cefaf330eb10fa4b98c21c48a9aa
parent9f14dd4d7c0cce154c5b13728d69a77c58103c44 (diff)
downloadpsutil-31324467736c9a0fc63f47f11c6810dee23f05e2.tar.gz
fix #1437: return pids() in sorted order
-rw-r--r--HISTORY.rst1
-rw-r--r--docs/index.rst8
-rw-r--r--psutil/__init__.py4
-rwxr-xr-xpsutil/tests/test_posix.py4
-rwxr-xr-xpsutil/tests/test_system.py8
5 files changed, 14 insertions, 11 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index c553086b..9fcc72f8 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -18,6 +18,7 @@ XXXX-XX-XX
- 1426_: [Windows] PAGESIZE and number of processors is now calculated on
startup.
- 1433_: new Process.parents() method. (idea by Ghislain Le Meur)
+- 1437_: pids() are returned in sorted order.
**Bug fixes**
diff --git a/docs/index.rst b/docs/index.rst
index 8983e880..a6f2ba73 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -817,13 +817,17 @@ Functions
.. function:: pids()
- Return a list of current running PIDs. To iterate over all processes
- and avoid race conditions :func:`process_iter()` should be preferred.
+ Return a sorted list of current running PIDs.
+ To iterate over all processes and avoid race conditions :func:`process_iter()`
+ should be preferred.
>>> import psutil
>>> psutil.pids()
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, ..., 32498]
+ .. versionchanged::
+ 5.6.0 PIDs are returned in sorted order
+
.. function:: process_iter(attrs=None, ad_value=None)
Return an iterator yielding a :class:`Process` class instance for all running
diff --git a/psutil/__init__.py b/psutil/__init__.py
index b5a584f2..1893298b 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -399,7 +399,7 @@ def _pprint_secs(secs):
@lru_cache()
def _first_pid():
- return sorted(pids())[0]
+ return pids()[0]
# =====================================================================
@@ -1500,7 +1500,7 @@ _as_dict_attrnames = set(
def pids():
"""Return a list of current running PIDs."""
- return _psplatform.pids()
+ return sorted(_psplatform.pids())
def pid_exists(pid):
diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py
index 955a4834..15d2f127 100755
--- a/psutil/tests/test_posix.py
+++ b/psutil/tests/test_posix.py
@@ -321,10 +321,8 @@ class TestSystemAPIs(unittest.TestCase):
def test_pids(self):
# Note: this test might fail if the OS is starting/killing
# other processes in the meantime
- pids_ps = ps("pid")
+ pids_ps = sorted(ps("pid"))
pids_psutil = psutil.pids()
- pids_ps.sort()
- pids_psutil.sort()
# on MACOS and OPENBSD ps doesn't show pid 0
if MACOS or OPENBSD and 0 not in pids_ps:
diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py
index b0b7e4f0..1236af3b 100755
--- a/psutil/tests/test_system.py
+++ b/psutil/tests/test_system.py
@@ -242,11 +242,11 @@ class TestSystemAPIs(unittest.TestCase):
self.assertFalse(psutil.pid_exists(pid), msg=pid)
def test_pids(self):
- plist = [x.pid for x in psutil.process_iter()]
- pidlist = psutil.pids()
- self.assertEqual(plist.sort(), pidlist.sort())
+ pidslist = psutil.pids()
+ procslist = [x.pid for x in psutil.process_iter()]
# make sure every pid is unique
- self.assertEqual(len(pidlist), len(set(pidlist)))
+ self.assertEqual(sorted(set(pidslist)), pidslist)
+ self.assertEqual(pidslist, procslist)
def test_test(self):
# test for psutil.test() function