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.
- Python | sympy.integrate() using limits
- Python | Size of string in memory
- Long Short Term Memory Networks Explanation
- Text Generation using Recurrent Long Short Term Memory Network
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- MySQL-Connector-Python module in Python
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.