The resource module in Python – A Beginner’s Introduction

Resource Python Min

Before you get started with this article, it is important to note that the resource module is a UNIX specific package, and will not work in the POSIX, i.e. Windows Operating System.

Introduction to the resource module

During the process of working with system monitoring and resources, we find ourselves wondering if there’s a better to monitor system information rather than manually go through all the system logs in the control panel.

A little more down the trail of forming an idea related to that concept, we come to a slight understanding that this might be possible, and completely feasible in the form of a script.

Well, looking a little further into the line of thought, it’s a great idea!

Automating the script with python-crontab, sched and the dateutil module, would provide for an automatic update log scheduled at a particular interval everyday, so, you don’t have to manually receive information at a particular time.

But, before trying to automate it, we’ll need something that can first provide you with this information, and that’s where the resource module comes into play.

Used for providing basic information regarding the system resources, and functionality to control it as well, the resource module is exactly what we’re looking for.

So, let’s cut to the chase, and get started with working on the module!

Using the resource module in Python

Being a part of the standard library in Python, the resource module is one that does not need to be installed separately, and this means that working with the module on a fresh new server or client with Python installed should come automatically without any issue.

However, it is reported that some versions of python do seem to face any issue with the resource module, and as such, it is recommended to install the resource module using the pip command.

pip install python-resources

Now that we’re done with that, we still need to use its components to retrieve the required information, so, let’s get importing!

1.0 Setting up the ecosystem

Before we begin working with the functionality that is provided to us by the resource module, we’ll need to first import the module.

# Importing functions from the resource module
from resource import *
import time

Now that we have the modules imported, we can now begin retrieving information on the system resources.

1.1 The underlying parameter usage

The functionality of the module mainly depends on the parameters that are provided to the function that returns the required information.

A few examples of these parameters are,

  • resource.RUSAGE_SELF – Resources consumed by calling process.
  • resource.RUSAGE_CHILDREN – Resources consumed by children processes.
  • resource.RUSAGE_BOTH – Resources consumed by current and children processes.
  • resource.RUSAGE_THREAD – Resources consumed by current thread.

All of these RUSAGE_* symbols are passed to the getrusage() function to specify which process information was requested for.

1.2 Demonstration

# Function used to retrieve information regarding
## Resources consumed by the current process or it's children
### Non CPU bound task

# Calling for the resources consumed by the current process.

### CPU bound task
for i in range(10**8):
    _ = 1+1

# Calling for the resources consumed by the current process.

# Output
# resource.struct_rusage(ru_utime=0.016, ru_stime=0.004, ru_maxrss=5216, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=732, ru_majflt=1, ru_nswap=0, ru_inblock=80, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=6, ru_nivcsw=9)

# resource.struct_rusage(ru_utime=14.176, ru_stime=0.02, ru_maxrss=5140, ru_ixrss=0, ru_idrss=0, ru_isrss=0, ru_minflt=730, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=1, ru_nivcsw=177)

The output that we receive is in the form of an resource class object, with all the necessary information requested for in the structure of the object.

Each of these output values in the structure are floating point values representing the amount of time spent in executing in user and system mode respectively.

In case you wish to find out more about each of the parameters or wish to look into the module as a whole, you might be interested in visiting the getrusage() section in the documentation page.

1.3 Moving forward

Working with this module should have provided you with an idea about the resources that can be retrieved by the resource module.

Extending this module and implementing it in a script would be to monitor the system processes and check into the resource consumption in regular intervals.

In case you wish to work with such an idea, it would be wise to look into various other modules like the psutil, sys, and os module to work with system processes.

In order to schedule the checks to be automatic, you might wish to look into working with the dateutil, sched and python-crontab modules.


The use cases for this module are mostly involved in working with the creation of scripts which tend to monitor the system’s functioning and processes.

If you wish to work with system processes, testing and monitoring, as mentioned in the previous section, the articles you should look into are the psutil, sys, os, and the dateutil module.