Windows 上的 python psutil 拒绝访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7349854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python psutil on windows gives access denied
提问by krisdigitx
os: windows professional
操作系统:Windows 专业版
i am trying to use psutil to get a list of processes and their cpu usage, i ran the script as administrator and it fails when it encounters process DymoPnpService.exe, what could be the issue?
我正在尝试使用 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()
AcroRd32.exe 0.0 AcroRd32.exe 0.0 DymoPnpService.exe
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')
more research:
更多研究:
strange i can run the program from the windows command prompt...but it fails in the python ide
奇怪的是,我可以从 Windows 命令提示符运行该程序……但它在 python ide 中失败了
采纳答案by chown
run this in a cmd.exe prompt: tasklist /FI "IMAGENAME eq DymoPnpService.exe" /V
and check the "User Name". If it is "NT AUTHORITY\SYSTEM" then it is probably intentionally not allowing even an Administrator account to get cpu times, %, etc of the proc.
在 cmd.exe 提示符下运行:tasklist /FI "IMAGENAME eq DymoPnpService.exe" /V
并检查“用户名”。如果它是“NT AUTHORITY\SYSTEM”,那么它可能是故意不允许管理员帐户获取 proc 的 CPU 时间、百分比等。
Grab a copy of Process Explorerand find the path of the process and check the Security Tab of the Preferences right click menu option. To fix you maybe able to edit the Owner or Permissions of the DymoPnpService.exe executable but this could cause unexpected issues in Windows.
获取进程资源管理器的副本并找到进程的路径并检查首选项右键单击菜单选项的安全选项卡。要解决此问题,您可以编辑 DymoPnpService.exe 可执行文件的所有者或权限,但这可能会导致 Windows 出现意外问题。
You can also just continue the loop if the process doesn't allow you to get details about it:
如果该过程不允许您获取有关它的详细信息,您也可以继续循环:
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()
回答by Giampaolo Rodolà
Starting from version 0.6.0 psutil on Windows will no longer raise AccessDenied for different process methods (cpu_percent() amongst them): https://groups.google.com/forum/?fromgroups#!topic/psutil/oxAd0BuAzt0%5B1-25%5D
从版本 0.6.0 开始,Windows 上的 psutil 将不再针对不同的进程方法(其中包括 cpu_percent())引发 AccessDenied:https://groups.google.com/forum/?fromgroups#!topic/psutil/oxAd0BuAzt0%5B1- 25%5D