How to find and list down all running processes in python:
To list down all the running process using python, we need to use one third party package. It is called psutil. This package provides different methods for different system operations. We will use the process_iter() method to list down all runnin processes with its id.
How to install psutil:
We can install psutil using pip.
pip install psutil
Running this command will install psutil in your project.
process_iter() method:
process_iter() method can be used to read all running processes. It sorts the processes based on their PIDs. We can read different process informations.
In this post, I will show you how to read the name and id of each processes.
Python program to print the id and name of all active processes:
Let’s take a look at the below program. Here, we are printing the process id and name of the process.
import psutil
if __name__ == '__main__':
for p in psutil.process_iter():
print('{} {}'.format(p.pid, p.name()))
It will print all the process name and ids as like below:
0 kernel_task
1 launchd
57 syslogd
58 UserEventAgent
60 com.crystalidea.
61 RTProtectionDaem
63 uninstalld
64 fseventsd
65 mediaremoted
68 systemstats
70 configd
72 powerd
....
....