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

Slow response time of psutil Process Iter #1061

Closed
ajikumarmp opened this issue May 10, 2017 · 8 comments
Closed

Slow response time of psutil Process Iter #1061

ajikumarmp opened this issue May 10, 2017 · 8 comments

Comments

@ajikumarmp
Copy link

ajikumarmp commented May 10, 2017

Hi,

I have used PSUTIL for solving a problem where I wanted to check if my application is already running on windows. I used the code below and that works but I see that I get response almost a minute or mote later, this I felt is too much time, are there any other methods by which this can be done faster or any other suggestions to get a faster result?

def listprocessRunning():
    for proc in psutil.process_iter():
        try:
            #print "Process Names", proc.name()
            if proc.name() == "MY APP":
                print "My APP is running"
        except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
            print " Permission error and cannot access"
@giampaolo
Copy link
Owner

giampaolo commented May 10, 2017

1 minute? That's very weird by just getting the process name(), unless you have thousands of processes running.

@ajikumarmp
Copy link
Author

ajikumarmp commented May 10, 2017 via email

@giampaolo
Copy link
Owner

What is it that takes so much time? name()? or process_iter()?
Here, try this:

import psutil, time, contextlib


@contextlib.contextmanager
def timeit(what):
    t = time.time()
    yield
    print("%-30s: %f secs" % (what, time.time() - t))


def main():
    ls = psutil.process_iter()
    while 1:
        with timeit("process_iter next()"):
            try:
                proc = next(ls)
            except StopIteration:
                break
        with timeit("name()"):
            proc.name()


main()

@ajikumarmp
Copy link
Author

ajikumarmp commented May 10, 2017 via email

@giampaolo
Copy link
Owner

It looks like the first output was run on a Pentium 1 as both process_iter() (which internally calls create_time() and name() are unbelievably slow. Is this an old PC?

@ajikumarmp
Copy link
Author

ajikumarmp commented May 10, 2017 via email

@giampaolo
Copy link
Owner

I doubt it has to do with multiple users connected.

@ajikumarmp
Copy link
Author

ajikumarmp commented May 12, 2017 via email

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

2 participants