What is psutil.pids() method in Python?
The psutil module
The psutil (Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). We mainly use it for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via pip as follows:
pip install psutil
The pids method
The pids method returns a list of all the process IDs of running processes in the system. This method only returns the process IDs.
Syntax
psutil.pids()
Parameters
The method takes no parameters.
Return value
The pids method returns a list of all the process IDs of running processes in the system.
Example
import psutilpids = psutil.pids()print("The pids of the processes running are as follows")print(pids)
Explanation
- Line 1: We import the
psutilmodule. - Lines 3–5: We retrieve the process IDs using
psutil.pids()method and print them to the console.