The docs for psutil.process_iter()
say:
Return an iterator yielding a Process class instance for all running processes on the local machine. Every instance is only created once and then cached into an internal table which is updated every time an element is yielded. emphasize is mine
where "running" is defined by .is_running()
method that returns True
for zombie processes. .is_running()
is safe to use even if the process is gone and its pid is reused.
My understanding is that _process.is_running()
is true at the moment psutil.process_iter()
yields it. In principle, before _process.as_dict
returns with the info, several things may happen:
- process may die -- become a zombie:
_process.is_running()
is true,pid
andstatus
are meaningful - it may be reaped -- dead-dead: the process info is unavailable (
psutil.NoSuchProcess
exception) or outdated (if cached) - its pid may be reused -- some
_process
methods may return incorrectly the info for the new process..is_running()
uses several process attributes so it should detect the case whenpid
is reused.
Normally, OS doesn't reuse pid
immediately therefore p.3 shouldn't occur frequently.