summaryrefslogtreecommitdiff
path: root/psutil
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-02-18 01:43:16 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2020-02-18 01:43:16 +0100
commitc9fc4fdadc5a19d97916cbf13b1b479834b94bd8 (patch)
treefa874c23d1ed93ec6022c6a88acaa2fbb98e3f79 /psutil
parent793148fee9c46c3df8b7ca941d6c73d5c61bc3a8 (diff)
downloadpsutil-c9fc4fdadc5a19d97916cbf13b1b479834b94bd8.tar.gz
revert #1667 process_iter() new_only param
On a second thought I realized that process_iter() uses a global variable, so it's not thread safe. That means that if the are 2 threads using it, the first thread one calling the function (+ consume the iterator), will "steal" the processes of the second thread. psutil.cpu_percent() has the same problem. That means we have a problem can't solve with the current API and requires a lot of thinking on how to solve it as it's not obvious.
Diffstat (limited to 'psutil')
-rw-r--r--psutil/__init__.py6
-rwxr-xr-xpsutil/tests/test_system.py16
2 files changed, 1 insertions, 21 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index a58b452c..2f4e147f 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1407,7 +1407,7 @@ _pmap = {}
_lock = threading.Lock()
-def process_iter(attrs=None, ad_value=None, new_only=False):
+def process_iter(attrs=None, ad_value=None):
"""Return a generator yielding a Process instance for all
running processes.
@@ -1428,8 +1428,6 @@ def process_iter(attrs=None, ad_value=None, new_only=False):
If *attrs* is an empty list it will retrieve all process info
(slow).
- If *new_only* is true this function will take into consideration
- only new PIDs which appeared since the last time it was called.
"""
def add(pid):
proc = Process(pid)
@@ -1452,8 +1450,6 @@ def process_iter(attrs=None, ad_value=None, new_only=False):
with _lock:
ls = list(dict.fromkeys(new_pids).items())
- if not new_only:
- ls += list(_pmap.items())
ls.sort()
for pid, proc in ls:
diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py
index c32e8a73..3834209f 100755
--- a/psutil/tests/test_system.py
+++ b/psutil/tests/test_system.py
@@ -105,22 +105,6 @@ class TestProcessAPIs(unittest.TestCase):
self.assertGreaterEqual(p.info['pid'], 0)
assert m.called
- def test_process_iter_new_only(self):
- ls1 = list(psutil.process_iter(attrs=['pid']))
- ls2 = list(psutil.process_iter(attrs=['pid'], new_only=True))
- self.assertGreater(len(ls1), len(ls2))
- # assume no more than 3 new processes were created in the meantime
- self.assertIn(len(ls2), [0, 1, 2, 3, 4, 5])
-
- sproc = get_test_subprocess()
- ls = list(psutil.process_iter(attrs=['pid'], new_only=True))
- self.assertIn(len(ls2), [0, 1, 2, 3, 4, 5])
- for p in ls:
- if p.pid == sproc.pid:
- break
- else:
- self.fail("subprocess not found")
-
@unittest.skipIf(PYPY and WINDOWS,
"get_test_subprocess() unreliable on PYPY + WINDOWS")
def test_wait_procs(self):