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.
What is Python's equivalent to 'ulimit'?
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))
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_MSGQUEUE
–
Kevyn © 2022 - 2024 — McMap. All rights reserved.
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