Skip to content

Commit

Permalink
fix #1437: return pids() in sorted order
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 26, 2019
1 parent 9f14dd4 commit 3132446
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
8 changes: 6 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def _pprint_secs(secs):

@lru_cache()
def _first_pid():
return sorted(pids())[0]
return pids()[0]


# =====================================================================
Expand Down Expand Up @@ -1500,7 +1500,7 @@ def wait(self, timeout=None):

def pids():
"""Return a list of current running PIDs."""
return _psplatform.pids()
return sorted(_psplatform.pids())


def pid_exists(pid):
Expand Down
4 changes: 1 addition & 3 deletions psutil/tests/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ def test_pid_exists_2(self):
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
Expand Down

0 comments on commit 3132446

Please sign in to comment.