【4】进程管理-11-修改当前系统资源限制--resources

resources (https://docs.python.org/2/library/resource.html)模块用于查询或修改当前系统资源限制设置.

一、使用 resource 模块查询当前设置

import resource  
  
print "usage stats", "=>", resource.getrusage(resource.RUSAGE_SELF)  
print "max cpu", "=>", resource.getrlimit(resource.RLIMIT_CPU)  
print "max data", "=>", resource.getrlimit(resource.RLIMIT_DATA)  
print "max processes", "=>", resource.getrlimit(resource.RLIMIT_NPROC)  
print "page size", "=>", resource.getpagesize()  

结果

usage stats => (0.03, 0.02, 0, 0, 0, 0, 75, 168, 0, 0, 0, 0, 0, 0, 0, 0)  
max cpu => (2147483647, 2147483647)  
max data => (2147483647, 2147483647)  
max processes => (256, 256)  
page size => 4096  

二、使用 resource 模块限制资源

import resource  
  
resource.setrlimit(resource.RLIMIT_CPU, (0, 1))  
  
# pretend we're busy  
for i in range(1000):  
	for j in range(1000):  
		for k in range(1000):  
			pass  

三、我的案例

cur_hdls, max_hdls = resource.getrlimit(resource.RLIMIT_NOFILE)
target_hdls = min(max_hdls, target_procs) if max_hdls > 0 else target_procs
resource.setrlimit(resource.RLIMIT_NOFILE, (max(cur_hdls, target_hdls), max_hdls))

参考资料:

http://blog.csdn.net/liangyuannao/article/details/9697987

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn