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 psutil
pids = psutil.pids()
print("The pids of the processes running are as follows")
print(pids)

Explanation

  • Line 1: We import the psutil module.
  • Lines 3–5: We retrieve the process IDs using psutil.pids() method and print them to the console.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources