Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Restrict CPU time or CPU Usage using python 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
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Restrict #CPU #time #CPU #Usage #python #code
ADD COMMENT
Topic
Name
8+2 =