2006年12月28日 星期四 10:28
想知道python有没自带的库, 可以获取到系统运行中的进程列表。 因为使用环境是windows, 而win本身没有"ps"之类的工具。 虽然有个叫tasklist的类似工具, 不过也只限于winxp环境, win2k里没有这个东西。
2006年12月28日 星期四 15:15
´ÓpythonwinÖÐÕÒµ½µÄ£¬Õâ¸öÊÇ×¢²áΪcom×é¼þµÄ´úÂ룬µ«°üº¬ÁË¡£¡£
import win32pdh, string, win32api
from win32com.server.exception import COMException
import win32com.server.util
import win32com.client.dynamic
#to generate guids use:
#import pythoncom
#print pythoncom.CreateGuid()
class pyperf:
# COM attributes.
_reg_clsid_ = '{763AE791-1D6B-11D4-A38B-00902798B22B}'
#guid for your class in registry
_reg_desc_ = "get process list and ids"
_reg_progid_ = "PyPerf.process" #The progid for this class
_public_methods_ = ['procids','proclist' ] #names of callable methods
def __init__(self):
self.object='process'
self.item='ID Process'
def proclist(self):
try:
junk, instances = win32pdh.EnumObjectItems(None,None,self.object,
win32pdh.PERF_DETAIL_WIZARD)
return instances
except:
raise COMException("Problem getting process list")
def procids(self):
#each instance is a process, you can have multiple processes w/same
name
instances=self.proclist()
proc_ids=[]
proc_dict={}
for instance in instances:
if proc_dict.has_key(instance):
proc_dict[instance] = proc_dict[instance] + 1
else:
proc_dict[instance]=0
for instance, max_instances in proc_dict.items():
for inum in xrange(max_instances+1):
try:
hq = win32pdh.OpenQuery() # initializes the query handle
path = win32pdh.MakeCounterPath(
(None,self.object,instance,
None, inum, self.item) )
counter_handle=win32pdh.AddCounter(hq, path) #convert
counter path to counter handle
win32pdh.CollectQueryData(hq) #collects data for the
counter
type, val =
win32pdh.GetFormattedCounterValue(counter_handle,
win32pdh.PDH_FMT_LONG)
proc_ids.append(instance+'\t'+str(val))
win32pdh.CloseQuery(hq)
except:
raise COMException("Problem getting process id")
proc_ids.sort()
return proc_ids
if __name__=='__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(pyperf)
2006/12/27, Young <fivesheep在gmail.com>:
>
> ÏëÖªµÀpythonÓÐû×Ô´øµÄ¿â£¬ ¿ÉÒÔ»ñÈ¡µ½ÏµÍ³ÔËÐÐÖеĽø³ÌÁÐ±í¡£
>
> ÒòΪʹÓû·¾³ÊÇwindows, ¶øwin±¾ÉíûÓÐ"ps"Ö®ÀàµÄ¹¤¾ß¡£ ËäÈ»Óиö½ÐtasklistµÄÀàËÆ¹¤¾ß£¬ ²»¹ýÒ²Ö»ÏÞÓÚwinxp»·¾³£¬
> win2kÀïûÓÐÕâ¸ö¶«Î÷¡£
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20061227/4ff38db5/attachment.html
2006年12月28日 星期四 15:48
pywmi这个模块比较简单,封装了windows的wmi库,你可以试试 -- 云电清华同方小民工
2006年12月29日 星期五 16:51
谢谢两位, 感觉用这些库比tasklist/taskkill麻烦太多了 刚才又搜索了一下, 有人说直接把xp上的tasklist/taskkill两个工具拷贝到win2k上就能用。 如果这个说法正确, 我就直接把这两个东西打包一起发布就算了。。 On 12/28/06, 笨笨狗 <chen.ruijie在gmail.com> wrote: > pywmi这个模块比较简单,封装了windows的wmi库,你可以试试 > > -- > 云电清华同方小民工 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号