Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge CPU Usage (appx 45%) on continuosly iterating psutil.process_iter() #90

Closed
giampaolo opened this issue May 23, 2014 · 2 comments

Comments

@giampaolo
Copy link
Owner

From [email protected] on June 08, 2010 15:07:02

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

Attachment: psutil_test.py

Original issue: http://code.google.com/p/psutil/issues/detail?id=90

@giampaolo
Copy link
Owner Author

From g.rodola on June 08, 2010 10:22:13

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

Status: Invalid

@giampaolo
Copy link
Owner Author

From g.rodola on July 06, 2010 13:38:29

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant