You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What steps will reproduce the problem?
1.Run attached test script (named psutil_test.py)
Here I am iterating (1000 times) over psutil.process_iter() to check whether a process named 'notepad.exe' is running or not.
What is the expected output?
What do you see instead?
While script was running I checked CPU usage of python.exe in
TaskManager it is appx 45 % and keeps in range of 40-48%.
What version of psutil are you using? On what operating system? 0.1.3 win32 python 2.5. Win XP SP3.
Please provide any additional information below.
1) If I put a delay of 1 sec then CPU usage is 5 % appx.
2) Might be there are some better ways to achieve what I am trying to achieve to check whether process is running or not.
Thanks
Having such a high CPU usage is normal if you don't sleep() for a while, hence this should not be considered a bug.
As for how to properly do this I think there are smarter ways, like
getting pids in a loop, check for new ones and only then instantiate psutil.Process() to check for the program name.
Please send a message on the ml and we'll discuss there: http://groups.google.com/group/psutil
Since you didn't send the message on the ml I'm going to reply here, just for the record. =)
I came up with this:
import psutil, time
def wait_for_process_by_name(name, poll_interval=0.1, timeout=None):
"""Wait for process with name "name" to be started
and return a Process() instance when this happens.
If "timeout" is specified OSError is raised if the process
doesn't appear within the time specified, which is expressed
in seconds.
"""
if timeout is not None:
raise_at = time.time() + timeout
else:
raise_at = None
while 1:
pids1 = psutil.get_pid_list()
time.sleep(poll_interval)
pids2 = psutil.get_pid_list()
new_pids = set(pids2) - set(pids1)
if new_pids:
new_pids = list(new_pids)
new_pids.sort()
for pid in new_pids:
print psutil.Process(pid).name
try:
p = psutil.Process(pid)
except psutil.NoSuchProcess:
# process is dead in meantime
continue
else:
if p.name == name:
return p
if raise_at is not None and time.time() >= raise_at:
raise OSError("timeout")
wait_for_process_by_name('gnome-terminal')
From [email protected] on June 08, 2010 15:07:02
Attachment: psutil_test.py
Original issue: http://code.google.com/p/psutil/issues/detail?id=90
The text was updated successfully, but these errors were encountered: