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.
Asked
Active
Viewed 4,613 times
5

Wesley Wang
- 67
- 1
- 5
-
The `resource` module is the right place. It just looks like it hasn't been updated to know about resource limits that were added in recent Linux versions. `RLIMIT_MSGQUEUE` was added in 2.6.8, `RLIMIT_RRTIME` in 2.6.12. – Barmar Jul 09 '19 at 20:16
-
You might be able to look up the values of the constants in the C header files and use them. – Barmar Jul 09 '19 at 20:17
1 Answers
6
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))

Barmar
- 741,623
- 53
- 500
- 612
-
Thanks! This worked out well, just have to make sure to catch `ValueError` in case the resource isn't defined on the OS. – Wesley Wang Jul 09 '19 at 20:49
-
1I found the constants are also defined within the `resource` module i.e. `resource.RLIMIT_MSGQUEUE` – user1330734 Aug 09 '20 at 13:04