Resource Usage Information using Python

Python's resource module helps monitor and control system resource usage in UNIX-based systems. It provides methods to get resource usage statistics and set resource limits to prevent processes from consuming excessive system resources.

Importing the Resource Module

First, import the resource module ?

import resource

Resource Limits

The module uses two types of limits: soft limits (current limit that can be changed) and hard limits (maximum value for soft limits). Soft limits cannot exceed hard limits, and hard limits can only be reduced.

Method resource.getrlimit(resource)

Returns the current soft and hard limits as a tuple ?

import resource

# Get CPU time limits
cpu_limits = resource.getrlimit(resource.RLIMIT_CPU)
print(f"CPU limits (soft, hard): {cpu_limits}")

# Get file descriptor limits
file_limits = resource.getrlimit(resource.RLIMIT_NOFILE)
print(f"File descriptor limits: {file_limits}")
CPU limits (soft, hard): (-1, -1)
File descriptor limits: (1024, 1048576)

Method resource.setrlimit(resource, limits)

Sets resource limits using a tuple of (soft_limit, hard_limit). Use RLIM_INFINITY for unlimited resources ?

import resource

# Set CPU time limit to 5 seconds (soft) and 10 seconds (hard)
resource.setrlimit(resource.RLIMIT_CPU, (5, 10))

# Check the updated limits
limits = resource.getrlimit(resource.RLIMIT_CPU)
print(f"New CPU limits: {limits}")
New CPU limits: (5, 10)

Resource Usage Statistics

Method resource.getrusage(who)

Returns detailed resource usage information for the current process or its children ?

import resource

# Get resource usage for current process
usage = resource.getrusage(resource.RUSAGE_SELF)

print(f"User time: {usage.ru_utime:.4f} seconds")
print(f"System time: {usage.ru_stime:.4f} seconds")
print(f"Max memory used: {usage.ru_maxrss} KB")
print(f"Page faults: {usage.ru_minflt}")
User time: 0.0250 seconds
System time: 0.0080 seconds
Max memory used: 9356 KB
Page faults: 1147

Method resource.getpagesize()

Returns the system page size in bytes ?

import resource

page_size = resource.getpagesize()
print(f"System page size: {page_size} bytes")
System page size: 4096 bytes

Common Resource Constants

Constant Description
RLIMIT_CPU Maximum CPU time in seconds
RLIMIT_DATA Maximum heap size
RLIMIT_STACK Maximum stack size
RLIMIT_NOFILE Maximum number of open files
RUSAGE_SELF Current process resource usage
RUSAGE_CHILDREN Child processes resource usage

Practical Example

Here's a complete example monitoring resource usage ?

import resource
import time

def monitor_resources():
    # Get initial usage
    start_usage = resource.getrusage(resource.RUSAGE_SELF)
    
    # Perform some work
    data = []
    for i in range(100000):
        data.append(i ** 2)
    
    # Get final usage
    end_usage = resource.getrusage(resource.RUSAGE_SELF)
    
    # Calculate differences
    user_time = end_usage.ru_utime - start_usage.ru_utime
    sys_time = end_usage.ru_stime - start_usage.ru_stime
    
    print(f"User CPU time used: {user_time:.4f} seconds")
    print(f"System CPU time used: {sys_time:.4f} seconds")
    print(f"Memory usage: {end_usage.ru_maxrss} KB")

monitor_resources()

Conclusion

The resource module provides essential tools for monitoring and controlling system resource usage in Python applications. Use getrusage() to track resource consumption and setrlimit() to prevent excessive resource usage.

Updated on: 2026-03-25T04:52:03+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements