在使用resource設(shè)置當(dāng)前進(jìn)程的MEM_LIMIT的時(shí)候, 發(fā)現(xiàn)在我的CentOS6x和7x上都不work了, 測(cè)試代碼如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import resource
resource.setrlimit(resource.RLIMIT_RSS, (1024, 1024))
s = ' ' * (10 * 1024 * 1024)
time.sleep(60)
查了一下資料, 總算在下面的鏈接中找到了答案.就是 RLIMIT_RSS 只在 Linux 2.4.x, x < 30的系統(tǒng)上工作. 具體說(shuō)明如下:
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 to madvise(2) specifying MADV_WILLNEED.
最后沒(méi)有辦法,只能自己周期性的計(jì)算內(nèi)存使用情況,來(lái)執(zhí)行相應(yīng)的操作了迎膜。
另外熟丸,resource模塊只能設(shè)置當(dāng)前進(jìn)程的rlimit,如果要設(shè)置任意進(jìn)程的rlimit,可以使用psutil模塊來(lái)實(shí)現(xiàn)。