Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Working with the psutil module in Python

Psutil Poster Min

Let’s talk about the psutil module in Python. A lot of the time working with system processes and profiling, we tend to desire an alternative to checking the system’s Task Manager to understand what the impact of our test creates.

Somewhere along the line of manually checking the Task Manager for information, we come to realize that we’re doing the same process over and over again.

Of course this doesn’t look good in the eyes of Don’t Repeat Yourself enthusiasts.

That being said, you’ll need to create a script that can go through the system processes and provide a report when you run the script.

This is where the psutil module comes into picture, providing features that are quite essential while working with system processes.

psutil is a module that is best utilized for system monitoringprofiling and limiting process resources and management of running processes.

Let’s get started working with it!

Installing the psutil module

The installation procedure for psutil using the pip system is pretty straightforward,

pip install psutil

In case you are using any other package manager, you might want to look into their documentation for psutil installation.

Now that we have the module set up and ready to go, we can get started with replacing the need to check the Task Manager altogether.

Using the psutil module in Python scripts

The psutil modules is one that contains a lot of methods which can be categorized in a few sections, system, processes, windows services, and constants. There also exist independent methods which can fall into the miscellaneous category.

There exist a large number of these methods and we’ll only be going through some of them in these sections, but, links to the documentation will be provided in the sections for each of the subsections.

1. System Information

psutil provides us with a large variety of functions that we can use to receive information regarding CPU, memory, disks, network, sensors, and other system information.

A few functions tested out from each of these sections, would provide us with the following code and output.

# Importing the module before utilization
import psutil

# Retrieving information regarding the CPU
## Returns the system CPU times as a named tuple
print(psutil.cpu_times())

## Returns the system-wide CPU utilization as a percentage
print(psutil.cpu_percent())

## Returns the number of logical CPUs in the system
print(psutil.cpu_count())

## Returns the various CPU statistics as a tuple
print(psutil.cpu_stats())

## Returns the CPU frequency as a nameduple
print(psutil.cpu_freq())

When these functions are printed out to console, we receive a log of the following sort,

scputimes(user=242962.0, system=84860.32812499994, idle=432883.46875, interrupt=5186.125, dpc=4331.65625)
0.0
4
scpustats(ctx_switches=2378452881, interrupts=1779121851, soft_interrupts=0, syscalls=3840585413)
scpufreq(current=2000.0, min=0.0, max=2601.0)

We’ve compiled a gist for a simple overview regarding the utilization of the psutil module, in case you wish to look into it.

There are a lot more functions that are a part of the module, which can be found over in the official documentation.

2. Processes

The functions that are available through the psutil module allow for Python to retrieve information regarding the processes that are currently being run throughout the system.

These processes have particular PID, or Process IDs that can be retrieved from the system, and we can use those in order to understand more about a specific process and their statistics.

Using the functionality of the module to work with processes, we can retrieve information regarding some processes in a simple manner,

# Importing the module before utilization
import psutil

# Returning a sorted list of currently running processes
print(psutil.pids())

# Returns an iterator which prevents the race condition for process stats
print(psutil.process_iter())

# Used to check whether a certain process exists in the current process list
print(psutil.pid_exists(0))

# An example to terminate and wait for the children
def on_terminate(proc):
    print("process {} terminated with exit code {}".format(proc, proc.returncode))

procs = psutil.Process().children()
for p in procs:
    p.terminate()
gone, alive = psutil.wait_procs(procs, timeout=3, callback=on_terminate)
for p in alive:
    p.kill()

A lot more of the functions that are useful for exception handling, as well as the utilization of the process class are documented and are worth checking out in case you wish to go through the parameters that can be worked with.

3. Windows Services

The psutil module provides us the functionality to check in on all the installed windows services as well.

The WindowsService class is the class which represents each Windows service by its name, and the details regarding the service are mainly retrieved using the win_service_iter() and the win_service_get() functions.

# Importing the module before utilization
import psutil

# Returns an iterator yielding a WindowsService class instance
print(psutil.win_service_iter())
# To provide the list of all processes contained we can use the list() function
print(list(psutil.win_service_iter()))

# Gets a Windows service by name, returning a WindowsService instance
print(psutil.win_service_get('WpnUserService_6b5d2'))

The way to work with the Windows services is provided in-depth in the official documentation of psutil, in case you wish to look more into the functions.

4. System Constants

The psutil module allows for a check in on System constants that provide a boolean response as to whether the particular constant is applicable on the operating system that you utilize.

In order to explain this, we can test it out using Python,

# Importing the module
import psutil

# Checking whether the operating system is Linux based
print(psutil.LINUX)

# Windows based OS?
print(psutil.WINDOWS)

In my case, the Operating System was a Windows based one, and as such, the response was False for LINUX, and True for Windows.

A lot more system constants can be accessed and verify checks for further proceeding of operations, to work with and identify which Operating System instructions to follow, and can be found in the Documentation.

Conclusion

Working with the psutil module is quite straightforward, and the applications for its usage are quite useful in order to display logging information and work with how resource consumption in your system takes place.

Check out our other articles, on the different modules you can use in Python to make your life easier – DataFrames, XML parsing, file compression.

References