How to Find and List All Running Processes in Python
In this tutorial, we are going to learn how to find and list all running processes in Python. It is a very simple program.
Python program to find and list all running processes
To do this, we need psutil library. Let us install it.
pip install psutil
Now, we need to import it into our program.
We have a method called process_iter which iterates all the running processes. This method is present within the psutil library. That’s the reason we have imported it into our program.
c=0 for process in psutil.process_iter (): c=c+1 Name = process.name () # Name of the process ID = process.pid # ID of the process print ("Process name =", Name ,",","Process ID =", ID) print ("\nTotal number of running process are ", c)
For every process that is running, we are getting its name and ID using the name and pid methods.
To count the total number of running processes, I used the variable ‘c’ and incremented it for every process.
Process name = System Idle Process , Process ID = 0 Process name = System , Process ID = 4 Process name = Registry , Process ID = 96 Process name = RuntimeBroker.exe , Process ID = 336 Process name = smss.exe , Process ID = 404 Process name = svchost.exe , Process ID = 448 Process name = csrss.exe , Process ID = 512 . . . Process name = chrome.exe , Process ID = 8864 Process name = svchost.exe , Process ID = 8880 Process name = svchost.exe , Process ID = 8936 Process name = svchost.exe , Process ID = 8948 Process name = csrss.exe , Process ID = 8980 Process name = ApntEx.exe , Process ID = 9132 Total number of running process are 145
The output varies from system to system. It depends on the processes that are running at that moment. At this moment there are 145 running processes in my system.
Also read, Python next() Function with examples