Skip to content

Commit c9fc4fd

Browse files
committed
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.
1 parent 793148f commit c9fc4fd

File tree

4 files changed

+2
-34
lines changed

4 files changed

+2
-34
lines changed

HISTORY.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ XXXX-XX-XX
1212
directory for additional data. (patch by Javad Karabi)
1313
- 1652_: [Windows] dropped support for Windows XP and Windows Server 2003.
1414
Minimum supported Windows version now is Windows Vista.
15-
- 1667_: added process_iter(new_only=True) parameter.
1615
- 1671_: [FreeBSD] add CI testing/service for FreeBSD (Cirrus CI).
1716
- 1677_: [Windows] process exe() will succeed for all process PIDs (instead of
1817
raising AccessDenied).

docs/index.rst

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ Functions
857857
.. versionchanged::
858858
5.6.0 PIDs are returned in sorted order
859859

860-
.. function:: process_iter(attrs=None, ad_value=None, new_only=False)
860+
.. function:: process_iter(attrs=None, ad_value=None)
861861

862862
Return an iterator yielding a :class:`Process` class instance for all running
863863
processes on the local machine.
@@ -873,8 +873,6 @@ Functions
873873
``info`` attribute attached to the returned :class:`Process` instances.
874874
If *attrs* is an empty list it will retrieve all process info (slow).
875875

876-
If *new_only* is true this function will take into consideration only
877-
new PIDs which appeared since the last time it was was called.
878876
Sorting order in which processes are returned is based on their PID.
879877

880878
Example::
@@ -898,18 +896,9 @@ Functions
898896
3: {'name': 'ksoftirqd/0', 'username': 'root'},
899897
...}
900898

901-
Get only new processes since last call::
902-
903-
>>> for proc in psutil.process_iter(['pid', 'name'], new_only=True):
904-
... print(proc.info)
905-
...
906-
907899
.. versionchanged::
908900
5.3.0 added "attrs" and "ad_value" parameters.
909901

910-
.. versionchanged::
911-
5.7.0 added "new_only" parameter.
912-
913902
.. function:: pid_exists(pid)
914903

915904
Check whether the given PID exists in the current process list. This is

psutil/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ def pid_exists(pid):
14071407
_lock = threading.Lock()
14081408

14091409

1410-
def process_iter(attrs=None, ad_value=None, new_only=False):
1410+
def process_iter(attrs=None, ad_value=None):
14111411
"""Return a generator yielding a Process instance for all
14121412
running processes.
14131413
@@ -1428,8 +1428,6 @@ def process_iter(attrs=None, ad_value=None, new_only=False):
14281428
If *attrs* is an empty list it will retrieve all process info
14291429
(slow).
14301430
1431-
If *new_only* is true this function will take into consideration
1432-
only new PIDs which appeared since the last time it was called.
14331431
"""
14341432
def add(pid):
14351433
proc = Process(pid)
@@ -1452,8 +1450,6 @@ def remove(pid):
14521450

14531451
with _lock:
14541452
ls = list(dict.fromkeys(new_pids).items())
1455-
if not new_only:
1456-
ls += list(_pmap.items())
14571453
ls.sort()
14581454

14591455
for pid, proc in ls:

psutil/tests/test_system.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,6 @@ def test_prcess_iter_w_attrs(self):
105105
self.assertGreaterEqual(p.info['pid'], 0)
106106
assert m.called
107107

108-
def test_process_iter_new_only(self):
109-
ls1 = list(psutil.process_iter(attrs=['pid']))
110-
ls2 = list(psutil.process_iter(attrs=['pid'], new_only=True))
111-
self.assertGreater(len(ls1), len(ls2))
112-
# assume no more than 3 new processes were created in the meantime
113-
self.assertIn(len(ls2), [0, 1, 2, 3, 4, 5])
114-
115-
sproc = get_test_subprocess()
116-
ls = list(psutil.process_iter(attrs=['pid'], new_only=True))
117-
self.assertIn(len(ls2), [0, 1, 2, 3, 4, 5])
118-
for p in ls:
119-
if p.pid == sproc.pid:
120-
break
121-
else:
122-
self.fail("subprocess not found")
123-
124108
@unittest.skipIf(PYPY and WINDOWS,
125109
"get_test_subprocess() unreliable on PYPY + WINDOWS")
126110
def test_wait_procs(self):

0 commit comments

Comments
 (0)