1

我正在尝试使用 psutil 获取 Windows 7 上进程的 PID,但我遇到了权限错误。我试过运行以管理员身份运行脚本的命令提示符,但这似乎没有任何效果。错误和相关代码都在下面。发生错误的行是尝试使用proc.name. 关于如何解决这个问题的任何建议?非常感谢!

错误:

Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 190, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 229, in get_process_exe
    return _convert_raw_path(_psutil_mswindows.get_process_exe(self.pid))
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "simple_address_retrieve.py", line 14, in <module>
    if proc.name == PROCNAME:
  File "C:\Python33\lib\site-packages\psutil\_common.py", line 48, in __get__
    ret = self.func(instance)
  File "C:\Python33\lib\site-packages\psutil\__init__.py", line 341, in name
    name = self._platform_impl.get_process_name()
  File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 190, in wrapper
    return fun(self, *args, **kwargs)
  File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 222, in get_process_name
    return os.path.basename(self.get_process_exe())
  File "C:\Python33\lib\site-packages\psutil\_psmswindows.py", line 194, in wrapper
    raise AccessDenied(self.pid, self._process_name)
psutil._error.AccessDenied: (pid=128)

代码:

PROCNAME = "MyProcessName.exe"

for proc in psutil.process_iter():
    if proc.name == PROCNAME:
        print(proc)
4

2 回答 2

2

不推荐使用 get_process_list() 使用 psutil.process_iter() 0.6.0 。同样在最新的 psutil 中,这个问题似乎已得到修复。您还可以继续迭代进程:

for proc in psutil.process_iter():
   try:
       if proc.name == PROCNAME:
          print(proc)
   except (PermissionError, AccessDenied):
       print "Permission error or access denied on process" # can't display name or id here

来自评论:

...并进行更多搜索,看来这是作者无法解决的问题(太复杂):http ://groups.google.com/forum/#!topic/psutil/EbdkIGlb4ls 。这个答案看起来是最好的方法。虽然没有 PermissionError,所以只需捕获 AccessDenied

于 2013-11-01T17:21:42.770 回答
0

除了 psutil.AccessDenied: # windows

例子

def test_children_duplicates(self):
        # find the process which has the highest number of children
        table = collections.defaultdict(int)
        for p in psutil.process_iter():
            try:
                table[p.ppid()] += 1
            except psutil.Error:
                pass
        # this is the one, now let's make sure there are no duplicates
        pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
        p = psutil.Process(pid)
        try:
            c = p.children(recursive=True)
        except psutil.AccessDenied:  # windows
            pass
        else:
            self.assertEqual(len(c), len(set(c))) 

参考:https ://www.programcreek.com/python/example/53869/psutil.process_iter

def find_process(regex):
    "If 'regex' match on cmdline return number and list of processes with his pid, name, cmdline."
    process_cmd_name = re.compile(regex)
    ls = []
    for proc in psutil.process_iter(attrs=['pid','name','cmdline']):
        try:
            if process_cmd_name.search(str(" ".join(proc.cmdline()))):
                ls.append(proc.info)
        except psutil.AccessDenied:  # windows
            pass
    return (ls)

与 psutil.AccessDenied 结合使用列表推导可能吗?

于 2019-12-12T18:24:03.573 回答