zoukankan      html  css  js  c++  java
  • python用win32pdh模块查看进程信息

    import win32pdh
    def get_processes():
        win32pdh.EnumObjects(None, None, win32pdh.PERF_DETAIL_WIZARD)
        #instances  是所有的进程名称
        junk, instances = win32pdh.EnumObjectItems(None,None,'Process', win32pdh.PERF_DETAIL_WIZARD)
    
        proc_dict = {}
        #建立进程状态字典并更新进程状态
        for instance in instances:
            if proc_dict.has_key(instance):
                proc_dict[instance] = proc_dict[instance] + 1
            else:
                proc_dict[instance]=0
        
        proc_ids = []
        for instance, max_instances in proc_dict.items():
            for inum in xrange(max_instances+1):
                hq = win32pdh.OpenQuery() # initializes the query handle 
                print hq
                try:
                    #查找出进程句柄位置
                    path = win32pdh.MakeCounterPath( (None, 'Process', instance, None, inum, 'ID Process') )
                   
                    counter_handle=win32pdh.AddCounter(hq, path) #convert counter path to counter handle
                    info = win32pdh.GetCounterInfo(counter_handle,3)
                    print info                
                    try:
                        win32pdh.CollectQueryData(hq) #collects data for the counter 
                        #val为进程id
                        type, val = win32pdh.GetFormattedCounterValue(counter_handle, win32pdh.PDH_FMT_LONG) 
                        print val
                        proc_ids.append((instance, val))
                    except win32pdh.error, e:
                        print e
                        pass
    
                    win32pdh.RemoveCounter(counter_handle)
    
                except win32pdh.error, e:
                    print e
                    pass
                win32pdh.CloseQuery(hq) 
    
        return proc_ids
  • 相关阅读:
    Python中使用MongoEngine
    Python中MongoDB使用
    JAVA 日期相关API (JDK 8 新增)
    JAVA 日期相关API(JDK 8 之前)
    StringBuffer 和StringBuilder
    String 类型转换
    String类常用方法
    JAVA String类
    关于线程锁的释放和保留
    java线程同步--使用线程池
  • 原文地址:https://www.cnblogs.com/slqt/p/5773452.html
Copyright © 2011-2022 走看看