os: windows professional
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?
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
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)
Ac开发者_如何学运维cessDenied: (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
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.
Grab a copy of Process Explorer and find the path of the process and check the Security Tab of the Preferences right click menu option. To fix you may be able to edit the Owner or Permissions of the DymoPnpService.exe executable but this could cause unexpected issues in 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()
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
精彩评论