5

I'm using psutil 2.1.2 on python 64 bit on windows 8.1. I'm using psutil.process_iter() to iterate over the running processes to get details on a specific process. for some reason I don't get the process even though it is displayed in the Task Manager and the Process Explorer

for proc in psutil.process_iter():
    try:
        if proc.name() == 'svchost.exe':  # patch for debugging 
            pass  #script never gets here
        opened_files = proc.open_files()
        opened_files = [opened_file[0] for opened_file in opened_files]
        if file_path in opened_files:
            processes.append(proc)
    except (psutil.AccessDenied, psutil.NoSuchProcess):
        pass

I checked the proc name and it is never the process I'm looking for. example of a process I don't see is svchost.exe

Thanks for the help!

Ido A
  • 87
  • 1
  • 1
  • 8
  • looking at this from another direction, using the process PID I got from `psutil.pids()`. `psutil.Process(pid).open_files()` resulted with `psutil.AccessDenied`. BTW process name was None for this PID in the proc object. Is there a way to work around this? – Ido A Oct 29 '14 at 14:49
  • file_path doesn't exist. – Scott Mar 29 '19 at 16:43

2 Answers2

4

For some (actually many) processes proc.open_files() will result in an AccessDenied exception so probably that is why you don't "see" all processes. Task manager and Process Explorer show more information than psutil because they have less privilege limitations (see: they can "extract" more info from the processes without bumping into 'access denied' errors). By using psutil you're able to see all processes (PIDs) though, only you won't be able to "query" all of them.

Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60
  • I added 2 lines to the code - script never gets to that point, though svchost.exe is running on the system. so I'm not sure that the proc.open_files() is the blame. It looks as the psutil.process_iter() doesn't return all the processes... – Ido A Oct 29 '14 at 13:39
  • proc.name() can also raise AccessDenied. If what you say is true (process_iter() does not return all processes) then "len(psutil.pids())" should differ from "len(list(psutil.process_iter())". – Giampaolo Rodolà Oct 29 '14 at 15:05
  • well, they are the same size as you say. I think the reason I did not see the process (or stop in that spot) is that the process name I saw when working with the PID was `None` so the if was False. Maybe `open_files()` just catches the exception and do nothing? – Ido A Oct 29 '14 at 15:48
1

Probably this bug https://github.com/giampaolo/psutil/issues/599 is what caused the problem. Fixed now.

Kashyap
  • 15,354
  • 13
  • 64
  • 103