windows上的python psutil拒绝访问

2024-04-25 21:09:31 发布

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

操作系统:windows professional

我正试图使用psutil获取进程及其cpu使用情况的列表,我以管理员身份运行脚本,当它遇到进程dymopnservice.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()

AcroRd32.exe 0.0版 AcroRd32.exe 0.0版 dymopnservice.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')

更多研究:

奇怪的是,我可以在windows命令提示符下运行这个程序……但是在python ide中失败了


Tags: nameinpygetmainlinecpuprocess
2条回答

从Windows上的0.6.0版psutil开始,将不再为不同的进程方法(其中的cpu_percent())引发访问拒绝: https://groups.google.com/forum/?fromgroups#!topic/psutil/oxAd0BuAzt0%5B1-25%5D

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

获取Process Explorer的副本并找到进程的路径,然后检查首选项右键单击菜单选项的“安全性”选项卡。要修复此问题,可以编辑dymopnservice.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()

相关问题 更多 >