使用WMI python计算正在运行的进程数很慢

2024-06-17 15:18:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一个“简单”的python程序,我做了一些计算机检查。 我正在使用wmi模块检索信息。然而,一个简单的计数线程用于计算正在运行的进程大约需要12到13秒。正在运行的进程数约为300个

我想知道我是否错过了什么,因为我有点惊讶这趟跑得这么慢

  • 操作系统:Windows 10企业版
  • Python:3.8.2 32位
  • wmi:1.5.1
  • wxpython:4.1.0

当按下一个按钮时,会启动多个线程,但大多数线程会立即返回数据,但这些线程不会循环,例如进程列表

要启动线程,我使用:

newObject = countProcess()
newObject.start()

类(线程)看起来像:

class countProcess(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        my_object = dict(status='Running', result='')        
        wx.CallAfter(pub.sendMessage, 'testListener', message=2,arg2=my_object)
        pythoncom.CoInitialize()
        d = wmi.WMI(find_classes=False)
        count = 0
        for process in d.Win32_Process():
            count +=1
        my_object = dict(status='idle', result=count)        
        wx.CallAfter(pub.sendMessage, 'testListener', message=2,arg2=my_object)
        #print (count)
        del d

我是否做错了什么,或者wmi进程的循环速度很慢,我必须接受这一点

提前谢谢


Tags: selfobject进程initmydefstatuscount
1条回答
网友
1楼 · 发布于 2024-06-17 15:18:14

好的,我找到了一种方法来加快速度。。。 通过只选择一个字段名,现在只需4秒,而不是12到13秒

更改:

for process in d.Win32_Process():

for process in d.Win32_Process(["Name"]):

我成功了! 我不知道这会产生这样的影响。 欢迎提出进一步加快速度的想法

相关问题 更多 >