Python resource module not work
(forgive my poor English, I am not native)
I tried Python resource module to limit child process.
It seems that setting RLIMIT_CPU can successfully limit the amount of cputime开发者_运维百科, but others like RLIMIT_RSS didn't work at all.
For example, I use following script to call child.py, and set RSS limit to (1024, 1024):
import os
import sys
import resource
import subprocess
def setlimits():
resource.setrlimit(resource.RLIMIT_RSS, (1024, 1024))
p = subprocess.Popen(["./child.py"], preexec_fn=setlimits)
print(p.wait())
child.py:
#!/usr/bin/env python3
import resource
print("RSS limit: ", resource.getrlimit(resource.RLIMIT_RSS))
a=[]
while True:
a.append(1) # deadloop, until eat up memory
Child process print "RSS limit: (1024, 1024)" and then continue to run until killed. I can see child.py eating my memory but RLIMIT_RSS didn't work.
My OS is latest Archlinux (2.6.39 kernel), and Python is ver3.2.
According to the documentation for setrlimit(), RLIMIT_RSS
has no effect in Linux 2.4.30 and later. Also, it only counts memory marked with madvise():
RLIMIT_RSS
Specifies the limit (in pages) of the process's resident set (the number of virtual pages resident in RAM). This limit only has effect in Linux 2.4.x, x < 30, and there only affects calls tomadvise()
specifyingMADV_WILLNEED
.
So, if you really want to limit processes this way, you'll have to run a 2.4 kernel and hack the Python interpreter so it calls madvise()
on allocated memory, which will probably have unexpected side-effects.
精彩评论