What is Python's equivalent to 'ulimit'?
Asked Answered
D

1

6

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.

Disputant answered 9/7, 2019 at 19:49 Comment(2)
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.Bracci
You might be able to look up the values of the constants in the C header files and use them.Bracci
B
7

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))
Bracci answered 9/7, 2019 at 20:22 Comment(2)
Thanks! This worked out well, just have to make sure to catch ValueError in case the resource isn't defined on the OS.Disputant
I found the constants are also defined within the resource module i.e. resource.RLIMIT_MSGQUEUEKevyn

© 2022 - 2024 — McMap. All rights reserved.