Python 等同于 'ulimit' 是什么?

What is Python's equivalent to 'ulimit'?

我正在尝试对当前 shell 的系统资源进行检查(基本上 ulimit) in Python to see if enough resources can be allocated. I've found the resource 模块中的所有内容,但它似乎没有所有信息 ulimit 提供(例如 POSIX message queuesreal-time priority)。有没有办法在不使用外部库的情况下在 Python 中找到这些的软硬限制?我想避免 运行 ulimit 如果可能的话,作为一个子流程,但如果这是唯一的方法,我会这样做。

使用resource.getrlimit()。如果 resource 包中没有常量,请在 /usr/include/bits/resource.h:

中查找
$ grep RLIMIT_MSGQUEUE /usr/include/bits/resource.h
  __RLIMIT_MSGQUEUE = 12,
#define RLIMIT_MSGQUEUE __RLIMIT_MSGQUEUE

那你可以自己定义常量:

import resource
RLIMIT_MSGQUEUE = 12
print(resource.getrlimit(RLIMIT_MSGQUEUE))