Python's resource Module Explained

How to use Python's built-in resource module to read and limit CPU time, memory, and file descriptors on Unix systems

If you landed here searching for “python resource module” and expected a tool directory, this is the page you actually wanted: Python’s built-in resource module, part of the standard library, used to read and control system resource usage on Unix-like systems (it does not work on Windows).

What it’s for

resource lets a running Python process inspect or limit how much of the system it’s allowed to consume — CPU time, memory, open file descriptors, stack size. It’s mostly used in two situations: measuring how much a script actually consumes (useful for debugging performance or memory issues), and enforcing hard limits so a runaway process can’t consume unlimited resources (common in sandboxing, batch job runners, and long-running services).

Reading current usage

import resource

usage = resource.getrusage(resource.RUSAGE_SELF)
print(f"Max memory used: {usage.ru_maxrss} KB")
print(f"User CPU time: {usage.ru_utime}s")
print(f"System CPU time: {usage.ru_stime}s")

RUSAGE_SELF reports on the current process; RUSAGE_CHILDREN reports on child processes spawned via fork/exec. Note that ru_maxrss is in kilobytes on Linux but bytes on macOS — a common source of confusion if you’re testing cross-platform.

Setting hard limits

import resource

# Limit this process to 512 MB of memory
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (512 * 1024 * 1024, hard))

# Limit CPU time to 10 seconds
resource.setrlimit(resource.RLIMIT_CPU, (10, 10))

Once a limit is set, the operating system enforces it — exceeding RLIMIT_CPU raises a SIGXCPU signal, and exceeding RLIMIT_AS (address space / virtual memory) causes allocation failures. This is a genuinely effective way to sandbox untrusted or unpredictable code without an external process manager, though it only limits the current process and anything it forks — it’s not a substitute for proper containerisation if you need real isolation.

Common limit types

  • RLIMIT_CPU — CPU time in seconds
  • RLIMIT_AS — total address space (effectively memory)
  • RLIMIT_NOFILE — maximum open file descriptors
  • RLIMIT_STACK — maximum stack size
  • RLIMIT_NPROC — maximum number of processes the user can create

A practical use case: preventing runaway scripts

If you’re running user-submitted code or untrusted scripts (a common pattern in coding-exercise platforms or CI sandboxes), setting RLIMIT_CPU and RLIMIT_AS before executing the script is a simple, effective safety net against infinite loops or memory leaks — far simpler than spinning up a full container for every execution, though less secure than proper isolation for genuinely adversarial input.

Looking for the site directory instead?

If you actually meant to find Python tools and libraries rather than the standard library module, browse Tools & Libraries or the full resource directory.

TopicsStandard LibraryUnixSystem Resources