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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.