Python | How to put limits on Memory and CPU Usage
This article aims to show how to put limits on the memory or CPU use of a program running. Well to do so, Resource module can be used and thus both the task can be performed very well as shown in the code given below:
Code #1 : Restrict CPU time
SIGXCPU signal is generated when the time expires on running this code and the program can clean up and exit.
Code #2 : In order to restrict memory use, the code puts a limit on the total address space
When no more memory is available then the program will start generating MemoryError exceptions with a memory limit.
How it works ?
- To set a soft and hard limit on a particular resource, setrlimit() function is used.
- The soft limit is a value upon which the operating system will notify the process via a signal or typically restrict it.
- An upper bound on the values is defined by the hard limit and it may be used for the soft limit.
- Although the hard limit can be lowered, it can never be raised by user processes and is controlled by a system-wide parameter set by the system administrator. (even if the process lowered itself).
- The setrlimit() function can additionally be used to set limits on things such as the number of child processes, number of open files, and similar system resources.
The code in this article only works on Unix systems, and that it might not work on all of them.