nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Add Handles count functionality (Windows) #195

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

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 reported on code.google.com by moden4dr...@gmail.com on 11 Aug 2011 at 10:40

GoogleCodeExporter commented 8 years ago
I originally added this feature as a private method for testing purposes:
http://code.google.com/p/psutil/source/detail?r=979#
...then removed it because the test was not reliable:
http://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?

Original comment by g.rodola on 19 Aug 2011 at 6:24

GoogleCodeExporter commented 8 years ago
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.

Original comment by moden4dr...@gmail.com on 11 Sep 2011 at 6:40

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 22 Oct 2011 at 11:45

GoogleCodeExporter commented 8 years ago
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".

Original comment by david.da...@gmail.com on 31 Oct 2011 at 3:44

GoogleCodeExporter commented 8 years ago
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:
http://code.google.com/p/psutil/source/browse/trunk/test/_windows.py?spec=svn129
1&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.

Original comment by g.rodola on 21 Apr 2012 at 5:56

GoogleCodeExporter commented 8 years ago
0.5.0 is finally out. Closing out as fixed.

Original comment by g.rodola on 27 Jun 2012 at 6:54

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Updated csets after the SVN -> Mercurial migration:
r1291 == revision d37b3525b133

Original comment by g.rodola on 2 Mar 2013 at 12:02