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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources