English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Using Python's resource usage information

To measure the usage of UNIX resources, we need to use the resource module in the program. The module can also control the resource utilization.

To use this module, we should use-import it

import resource

resource limits

In this module, we can usesetrlimit()to limit resource utilization. There are two parameters that can limit resources. These parameters are soft limit and hard limit. The soft limit is basically the current limit, which can be changed in the process, but cannot exceed the hard limit. The hard limit can be reduced to any value above the soft limit, but cannot be increased.

There are some methods and constants related to resource limits. These are.

method resource.getrlimit(resource)

This method is used to return the soft limit and hard limit as a tuple. If the specified resource is invalid, it will raise a ValueError.

method resource.setrlimit(resource, limit)

This method is used to set resource limits. The limit can be specified as a tuple of soft limit and hard limit. We can also use RLIM_INFINITY to create infinite resources.

method resource.prlimit(pid, resource[, limit])

This method issetrlimit()andgetrlimit()The combination of methods. It can get and set resource limits for any process at the same time. When pid is 0, it will work on the current process.

Some constants related to resource limits-

NumberConstants and Descriptions
1

RLIM_INFINITY

The limit of infinite resources

2

RLIMIT_CORE

The maximum size of the core file created by the current process.

3

RLIMIT_CPU

The maximum processor time for the processor. When the limit is exceeded, the SIGXCPU signal will be sent to the process.

4

RLIMIT_DATA

The maximum size of the processor heap

5

RLIMIT_STACK

The maximum size of the call stack. It uses the main thread's stack for multithreading processing.

6

RLIMIT_NOFILE

The maximum number of open file descriptors for the current process.

7

RLIMIT_MEMLOCK

The maximum address space for locked memory

8

RLIMIT_NICE

The upper limit of the best level of the process

9

RLIMIT_SWAP

The maximum size of swap space

10

RLIMIT_NTPS

The maximum number of pseudo terminals in the system

Resource usage

There are methods and constants related to resource usage.

method resource.getrusage(who)

This method is used to return the objects used by the current process or its child processes. It returns different fields. From the fields of the returned value, we can obtain information about how resources are used.

Method resource.getpagesize()

This method is used to return the number of bytes in the system page. It may be different from the physical page size.

Some constants related to resource usage.

NumberConstants and Descriptions
1

RUSAGE_SELF

It helps to request the resources consumed by the calling process. It is the sum of all resources of different threads.

2

RUSAGE_CHILDREN

It helps to request the resources consumed by the child processes.

3

RUSAGE_BOTH

It helps to request the resources consumed by the calling process and its child processes. Not applicable to all systems

4

RUSAGE_THREAD

Current thread's resource usage. Not applicable to all systems

Example Code

import resource
res_limits = resource.getrusage(resource.RUSAGE_SELF)
print(res_limits)
print('Page Size: ') + str(resource.getpagesize()))
resource.setrlimit(resource.RLIMIT_CPU, (1, 2))
print('Resource Limits: ') + str(resource.getrlimit(resource.RLIMIT_CPU)))
for a in range(1000):
   for b in range(1000):
      for c in range(1000):
         pass

Output Result

$ python3 example.py
resource.struct_rusage(ru_utime=0.035801, ru_stime=0.01074, ru_maxrss=9356, ru_ixrss=0, ru_idrss=0, 
ru_isrss=0, ru_minflt=1147, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0, 
ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=0, ru_nivcsw=17)
Page Size: 4096
Resource Limits: (1, 2)
CPU time limit exceeded (core dumped)