Python 与 'ulimit' 的等价物是什么? [英] What is Python's equivalent to 'ulimit'?
问题描述
我正在尝试检查当前 shell 的系统资源(基本上是 ulimit
) 以查看是否可以分配足够的资源.我找到了 resource
模块,但是它似乎没有ulimit
提供的所有信息(例如POSIX 消息队列
和实时优先级
).有没有办法在不使用外部库的情况下在 Python 中找到这些的软限制和硬限制?如果可能,我想避免将 ulimit
作为子进程运行,但如果这是唯一的方法,我会这样做.
I'm trying to implement a check on system resources for the current shell (basically everything in ulimit
) in Python to see if enough resources can be allocated. I've found the resource
module, but it doesn't seem to have all the information ulimit
provides (e.g. POSIX message queues
and real-time priority
). Is there a way to find the soft and hard limits for these in Python without using external libraries? I'd like to avoid running ulimit
as a subprocess if possible but if it's the only way, will do so.
推荐答案
使用 resource.getrlimit()
.如果resource
包中没有常量,在/usr/include/bits/resource.h
中查找:
Use resource.getrlimit()
. If there's no constant in the resource
package, look it up in /usr/include/bits/resource.h
:
$ grep RLIMIT_MSGQUEUE /usr/include/bits/resource.h
__RLIMIT_MSGQUEUE = 12,
#define RLIMIT_MSGQUEUE __RLIMIT_MSGQUEUE
然后你可以自己定义常量:
Then you can define the constant yourself:
import resource
RLIMIT_MSGQUEUE = 12
print(resource.getrlimit(RLIMIT_MSGQUEUE))
这篇关于Python 与 'ulimit' 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!