Xuan Tung Vu Xuan Tung Vu - 3 months ago 15
Python Question

Why doesn't ulimit -t kill a process sleeping beyond the limit with time.sleep?

I am trying to limit the CPU usage of one python script using

ulimit -t
. The script contains one
time.sleep()
statement and it is not killed after the specified time limit. Here is the simplified python script named
test.py
:

import time

while True:
time.sleep(0.1)


and I run the command as following:

ulimit -v 400000; ulimit -t 30; python test.py


The script keeps running forever. Are there any explanations for this? Thanks.

Answer

ulimit -t sets the CPU time limit. While your program is sleeping it doesn't use any CPU time, so that time doesn't count. It will only occupy a few CPU cycles to go to sleep again, that's why it's not killed.

You can't specify a real time limit using ulimit.