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

Add Handles count functionality (Windows) #195

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

Add Handles count functionality (Windows) #195

giampaolo opened this issue May 23, 2014 · 7 comments

Comments

@giampaolo
Copy link
Owner

From [email protected] on August 11, 2011 12:40:01

It is often need to count/monitor a handles opened by process.
Now use Pythoncom for this. Code example:

def getHandleCount(self):
    pid = self.PID
    try:
        pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
    except pythoncom.com_error:
        # already initialized.
        pass
    if psutil.pid_exists(pid):
        proc_name = psutil.Process(pid).name
        print proc_name
        strComputer = "."
        objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
        objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
        colItems = objSWbemServices.ExecQuery("SELECT * FROM 
Win32_PerfFormattedData_PerfProc_Process")
        for objItem in colItems:
            if objItem.IDProcess != pid:
                continue
            if objItem.HandleCount != None:
                self.HandleCount = objItem.HandleCount
                return objItem.HandleCount
    else:
        self.HandleCount = 0
        return 0

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

@giampaolo
Copy link
Owner Author

From g.rodola on August 19, 2011 11:24:47

I originally added this feature as a private method for testing purposes: 
https://code.google.com/p/psutil/source/detail?r=979 ...then removed it because 
the test was not reliable: 
https://code.google.com/p/psutil/source/detail?r=1056 I wasn't sure whether it 
would have been useful (that's why it was private).
Out of curiosity: what do you need this for?

@giampaolo
Copy link
Owner Author

From [email protected] on September 10, 2011 23:40:13

I am a QA. I've used it for monitor resources leaks. Handles count leaks are 
very important for my customer. Currently this only one feature that I use not 
from psutil.
In any case, thanks for your work.

@giampaolo
Copy link
Owner Author

From g.rodola on October 22, 2011 04:45:14

Status: WaitingForReview
Labels: -Type-Defect Type-Enhancement OpSys-Windows

@giampaolo
Copy link
Owner Author

From [email protected] on October 31, 2011 08:44:36

Windows uses handles for references to operating system objects. Objects like 
threads, files, and sockets all get handles.

Being able to keep a handle count in windows can be vital as a reported 
statistic under windows. There are many places in windows software where a 
handle leak can be used to identify a file that wasn't closed, or a socket that 
wasnt cleaned up properly, etc. Using these numbers a programmer can then look 
at the currently opened handles using a tool like process explorer to track 
down what type of object is leaking and fix the problem.

The only request I would make for the old code is changing "handlers" to "handles".

@giampaolo
Copy link
Owner Author

From g.rodola on April 21, 2012 10:56:24

This is now committed in r1291 .
As for my previous comment:

> [...] I removed it because the test was not reliable

I think the test was not accurate. I rewrote it here: 
https://code.google.com/p/psutil/source/browse/trunk/test/_windows.py?spec=svn1291&r=1291#218
 ...and it seems to work now.
Very sporadically it fails but I suppose that's because Windows might need 
sometime to actually close the process handle.

Summary: Add Handles count functionality (Windows)
Status: FixedInSVN
Labels: Milestone-0.5.0

@giampaolo
Copy link
Owner Author

From g.rodola on June 27, 2012 11:54:03

0.5.0 is finally out. Closing out as fixed.

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 04:02:37

Updated csets after the SVN -> Mercurial migration: r1291 == revision 
d37b3525b133

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