Windows上的python psutil允许访问被拒绝

kri*_*itx 5 python windows

os:windows专业版

我正在尝试使用psutil获取进程列表及其cpu使用情况,我以管理员身份运行脚本,遇到进程DymoPnpService.exe时失败,可能是什么问题?

import psutil

def process():
    plist = psutil.get_process_list()
    plist = sorted(plist, key=lambda i: i.name)
    for i in plist:
        print i.name, i.get_cpu_percent()

def main():
    process()


main()
Run Code Online (Sandbox Code Playgroud)

AcroRd32.exe 0.0 AcroRd32.exe 0.0 DymoPnpService.exe

Traceback (most recent call last):
  File "C:\Users\krisdigitx\Documents\windowsutil.py", line 13, in <module>
    main()
  File "C:\Users\krisdigitx\Documents\windowsutil.py", line 10, in main
    process()
  File "C:\Users\krisdigitx\Documents\windowsutil.py", line 7, in process
    print i.name, i.get_cpu_percent()
  File "C:\Python27\lib\site-packages\psutil\__init__.py", line 330, in get_cpu_percent
    pt1 = self._platform_impl.get_cpu_times()
  File "C:\Python27\lib\site-packages\psutil\_psmswindows.py", line 125, in wrapper
    raise AccessDenied(self.pid, self._process_name)
AccessDenied: (pid=1832, name='DymoPnpService.exe')
Run Code Online (Sandbox Code Playgroud)

更多研究:

奇怪我可以从Windows命令提示符运行该程序...但它在python ide中失败

cho*_*own 5

在cmd.exe提示符下运行:tasklist /FI "IMAGENAME eq DymoPnpService.exe" /V并检查"用户名".如果它是"NT AUTHORITY\SYSTEM"那么它可能故意不允许甚至管理员帐户获得proc的cpu时间,%等.

获取Process Explorer的副本并找到该过程的路径,然后选中Preferences右键菜单选项的Security选项卡.要解决此问题,您可以编辑DymoPnpService.exe可执行文件的所有者或权限,但这可能会导致Windows中出现意外问题.


如果进程不允许您获取有关它的详细信息,您也可以继续循环:

import psutil

def process():
    plist = psutil.get_process_list()
    plist = sorted(plist, key=lambda i: i.name)
    for i in plist:
        try:
            print i.name, i.get_cpu_percent()
        except AccessDenied:
            print "'%s' Process is not allowing us to view the CPU Usage!" % i.name

def main():
    process()

main()
Run Code Online (Sandbox Code Playgroud)