覺得這是我的第一個問題我也問過在這裏通常會找到我需要的所有答案,聚集了GDI和用戶對象性能指標(所以在此先感謝)我如何使用python
確定我的問題,我已經寫一個將在線程中監控一個進程並將結果輸出到一個csv文件供以後使用的python程序。此代碼運行良好我正在使用win32pdhutil計數器和WMI,Win32_PerfRawData_PerfProc_Process CPU%時間。我現在被要求監視一個WPF應用程序,特別是監視用戶對象和GDI對象。
這是我遇到問題的地方,就是我似乎無法找到任何python支持來收集這兩個計數器上的指標。這兩個計數器在任務管理器中很容易獲得,我覺得很奇怪,這兩個計數器的信息很少。我專門研究收集這些信息以查看是否有內存泄漏,我不想在已安裝的python以外的其他系統上安裝任何其他內容。請幫助您尋找解決方案。
我使用python 3.3.1,這將在Windows平臺上(主要是Win7和Win8的) 這是我用來收集數據
def gatherIt(self,whoIt,whatIt,type,wiggle,process_info2):
#this is the data gathering function thing
data=0.0
data1="wobble"
if type=="counter":
#gather data according to the attibutes
try:
data = win32pdhutil.FindPerformanceAttributesByName(whoIt, counter=whatIt)
except:
#a problem occoured with process not being there not being there....
data1="N/A"
elif type=="cpu":
try:
process_info={}#used in the gather CPU bassed on service
for x in range(2):
for procP in wiggle.Win32_PerfRawData_PerfProc_Process(name=whoIt):
n1 = int(procP.PercentProcessorTime)
d1 = int(procP.Timestamp_Sys100NS)
#need to get the process id to change per cpu look...
n0, d0 = process_info.get (whoIt, (0, 0))
try:
percent_processor_time = (float (n1 - n0)/float (d1 - d0)) *100.0
#print whoIt, percent_processor_time
except ZeroDivisionError:
percent_processor_time = 0.0
# pass back the n0 and d0
process_info[whoIt] = (n1, d1)
#end for loop (this should take into account multiple cpu's)
# end for range to allow for a current cpu time rather that cpu percent over sampleint
if percent_processor_time==0.0:
data=0.0
else:
data=percent_processor_time
except:
data1="N/A"
else:
#we have done something wrong so data =0
data1="N/A"
#endif
if data == "[]":
data=0.0
data1="N/A"
if data == "" :
data=0.0
data1="N/A"
if data == " ":
data=0.0
data1="N/A"
if data1!="wobble" and data==0.0:
#we have not got the result we were expecting so add a n/a
data=data1
return data
歡呼
代碼運行編輯爲正確的CPU時機問題,如果有人試圖運行它:D
哇,沒有人能解決這個問題,它比我想象的更困難... – trevellyon 2013-04-10 12:42:37