This module in python provides basic mechanisms for measuring and controlling system resources utilized by a program. And the symbolic constants are used to specify particular system resources and to request usage information about either the current process or its children. If there is a syscall failure, the OSError is raised.
The functions defined here are related to the limits assigned to the resources. Resource limits can be set for individual processes and the processes they start i.e. their child processes. These limits are hard and soft limits. So what are soft limits and hard limits?
There are 2 types of resource limits set by 'ulimit' command: hard limits and soft limits.
The soft limits are the ones that actually affect processes and hard limits are the maximum values for soft limits. Any user or process can raise the soft limits up to the value of the hard limits. If the hard limits needs to be changed, it can only be done by the processes with superuser authority.
resource.getrlimit(resource):
This function in python resource library returns a tuple with the current soft and hard limits of the resources as (soft limits, hard limits). It returns the ValueError if the resource provided as the argument is invalid. It returns an error if the system call fails unexpectedly.
resource.setrlimit(resource, limits):
This function in the python library resource sets the new limits of consumption of resource. It sends the limits to the function in the form of a tuple as (soft limits, hard limits) which contains two integers describing the new limits. A value RLIM_INFINITY can be used to request a limits that is unlimited.
It raises a ValueError if an invalid resource is specified as the argument and passed to the function, or if the soft limit exceeds the hard limit value (like if you specify the soft limit as RLIM_INFINITY but the hard limits is not specified as unlimited then ValueError is raised). A process with effective UID(Unique Identifier) of the super user can request any valid limit value, including unlimited, within the system imposed limit, otherwise ValueError will be raised if the requested limit exceeds the system imposed limit.
It raises error if the underlying system call fails.
resource.prlimit(pid, resource[;limits]):
This function in the resource library of python combines the setrlimit() and getrlimit() functions in single function and supports to get and set the resource limits of a arbitrary. If pid(Process ID) is 0, then the call applies to the current process. resource and limits have the same meaning as in setrlimit(), except that limits is optional.
When limits is not given the function returns the resource limit of the process pid. When limits is given the resource limit of the process is set and the former resource limit is returned.
It raises ProcessLookupError when pid can’t be found and PermissionError when the user doesn’t have CAP_SYS_RESOURCE for the process.
Comments