My Goal: I need to create a function that retrieves the PID and Name of each process and returns them in a dictionary. I will be uploading this to Django and really need two different dictionaries. 1 with every running process name and PID and another with all installed names and PIDs. So far I can get it to print the dictionary well but not return correctly. Maybe I am calling it wrong. I am newer to this.
import psutil
def pid_name():
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name'])
except psutil.NoSuchProcess:
pass
else:
return pinfo
If you were to sub "return" for print the dict will print fine. How to I call that dict from here? Is it even returning correctly??