January 2019
Beginner
318 pages
8h 23m
English
In this section, we will learn about how we can limit CPU and memory usage. First, we will write a script for putting a limit on CPU usage. Create a script called put_cpu_limit.py and write the following code in it:
import resourceimport sysimport signalimport timedef time_expired(n, stack): print('EXPIRED :', time.ctime()) raise SystemExit('(time ran out)')signal.signal(signal.SIGXCPU, time_expired)# Adjust the CPU time limitsoft, hard = resource.getrlimit(resource.RLIMIT_CPU)print('Soft limit starts as :', soft)resource.setrlimit(resource.RLIMIT_CPU, (10, hard))soft, hard = resource.getrlimit(resource.RLIMIT_CPU)print('Soft limit changed to :', soft)print()# Consume some CPU time in a pointless exercise ...Read now
Unlock full access