8.12.1 Resource Limits

Resources usage can be limited using the setrlimit() function described below. Each resource is controlled by a pair of limits: a soft limit and a hard limit. The soft limit is the current limit, and may be lowered or raised by a process over time. The soft limit can never exceed the hard limit. The hard limit can be lowered to any value greater than the soft limit, but not raised. (Only processes with the effective UID of the super-user can raise a hard limit.)

The specific resources that can be limited are system dependent. They are described in the getrlimit(2) man page. The resources listed below are supported when the underlying operating system supports them; resources which cannot be checked or controlled by the operating system are not defined in this module for those platforms.

getrlimit (resource)
Returns a tuple (soft, hard) with the current soft and hard limits of resource. Raises ValueError if an invalid resource is specified, or error if the underyling system call fails unexpectedly.

setrlimit (resource, limits)
Sets new limits of consumption of resource. The limits argument must be a tuple (soft, hard) of two integers describing the new limits. A value of -1 can be used to specify the maximum possible upper limit.

Raises ValueError if an invalid resource is specified, if the new soft limit exceeds the hard limit, or if a process tries to raise its hard limit (unless the process has an effective UID of super-user). Can also raise error if the underyling system call fails.

These symbols define resources whose consumption can be controlled using the setrlimit() and getrlimit() functions described below. The values of these symbols are exactly the constants used by C programs.

The Unix man page for getrlimit(2) lists the available resources. Note that not all systems use the same symbol or same value to denote the same resource.

RLIMIT_CORE
The maximum size (in bytes) of a core file that the current process can create. This may result in the creation of a partial core file if a larger core would be required to contain the entire process image.

RLIMIT_CPU
The maximum amount of CPU time (in seconds) that a process can use. If this limit is exceeded, a SIGXCPU signal is sent to the process. (See the signal module documentation for information about how to catch this signal and do something useful, e.g. flush open files to disk.)

RLIMIT_FSIZE
The maximum size of a file which the process may create. This only affects the stack of the main thread in a multi-threaded process.

RLIMIT_DATA
The maximum size (in bytes) of the process's heap.

RLIMIT_STACK
The maximum size (in bytes) of the call stack for the current process.

RLIMIT_RSS
The maximum resident set size that should be made available to the process.

RLIMIT_NPROC
The maximum number of processes the current process may create.

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

RLIMIT_OFILE
The BSD name for RLIMIT_NOFILE.

RLIMIT_MEMLOC
The maximm address space which may be locked in memory.

RLIMIT_VMEM
The largest area of mapped memory which the process may occupy.

RLIMIT_AS
The maximum area (in bytes) of address space which may be taken by the process.

guido@python.org