Open In App

Python | How to put limits on Memory and CPU Usage

Last Updated : 24 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




# importing libraries
import signal
import resource
import os
  
# checking time limit exceed
def time_exceeded(signo, frame):
    print("Time's up !")
    raise SystemExit(1)
  
def set_max_runtime(seconds):
    # setting up the resource limit
    soft, hard = resource.getrlimit(resource.RLIMIT_CPU)
    resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard))
    signal.signal(signal.SIGXCPU, time_exceeded)
  
# max run time of 15 millisecond
if __name__ == '__main__':
    set_max_runtime(15)
    while True:
        pass


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




# using resource 
import resource
  
def limit_memory(maxsize):
    soft, hard = resource.getrlimit(resource.RLIMIT_AS)
    resource.setrlimit(resource.RLIMIT_AS, (maxsize, hard))


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads