Python resource module RLIMIT_RSS not work

时间:2022-05-14
本文章向大家介绍Python resource module RLIMIT_RSS not work,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在使用resource设置当前进程的MEM_LIMIT的时候, 发现在我的CentOS6x和7x上都不work了, 测试代码如下:

#!/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的系统上工作. 具体说明如下:

https://linux.die.net/man/2/prlimit

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.

最后没有办法,只能自己周期性的计算内存使用情况,来执行相应的操作了。

另外,resource模块只能设置当前进程的rlimit,如果要设置任意进程的rlimit,可以使用psutil模块来实现。

随机文章