On this page
resource
Prev: reprlib | Up: stdlib_module | Next: rlcompleter
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — resource
Tags: #public #python3 #stdlib
Import
import resource
Overview
POSIX process resource usage and soft/hard limit controls (getrusage, getrlimit, setrlimit, RLIMIT_NOFILE, RLIMIT_CPU, RLIMIT_AS). Reach for resource to measure peak memory (ru_maxrss) and CPU time in benchmarks, or to enforce OS-level ceilings on worker subprocesses.
Quick Start
import resource
usage = resource.getrusage(resource.RUSAGE_SELF)
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
assert usage.ru_utime >= 0.0 and soft > 0
Implementation Walkthrough
1. Portable Peak Resident Memory (ru_maxrss) Helper
Note that macOS reports ru_maxrss in bytes, while Linux reports it in kilobytes:
import resource
import sys
def peak_rss_mib() -> float:
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
divisor = (1024 * 1024) if sys.platform == "darwin" else 1024
return rss / divisor
assert peak_rss_mib() > 0.0
Gotchas & Boundaries
- POSIX-only: Not available on Windows.
ru_maxrssunits differ by OS: Bytes on macOS/Darwin, kilobytes on Linux.- Lowering a hard limit is irreversible for non-root processes; adjust only the soft limit when you may need to raise it again later.
Common Functions
- resource.getpagesize() —
getpagesize()—resource.getpagesize()function. - resource.getrlimit() —
getrlimit(resource, /)—resource.getrlimit()function. - resource.getrusage() —
getrusage(who, /)—resource.getrusage()function. - resource.setrlimit() —
setrlimit(resource, limits, /)—resource.setrlimit()function.
Classes & Objects
- resource.error — Base class for I/O related errors. (Methods:
.add_note(),.with_traceback()) - resource.struct_rusage — struct_rusage: Result from getrusage. (Methods:
.count(),.index())
Constants & Attributes
resource.RLIMIT_AS(int) =5resource.RLIMIT_CORE(int) =4resource.RLIMIT_CPU(int) =0resource.RLIMIT_DATA(int) =2resource.RLIMIT_FSIZE(int) =1resource.RLIMIT_MEMLOCK(int) =6resource.RLIMIT_NOFILE(int) =8resource.RLIMIT_NPROC(int) =7resource.RLIMIT_RSS(int) =5resource.RLIMIT_STACK(int) =3resource.RLIM_INFINITY(int) =9223372036854775807resource.RUSAGE_CHILDREN(int) =-1resource.RUSAGE_SELF(int) =0
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/reprlib
- tuts/python3/stdlib_module/resource/error
- tuts/python3/stdlib_module/resource/getpagesize
- tuts/python3/stdlib_module/resource/getrlimit
- tuts/python3/stdlib_module/resource/getrusage
- tuts/python3/stdlib_module/resource/setrlimit
- tuts/python3/stdlib_module/resource/struct_rusage
- tuts/python3/stdlib_module/rlcompleter
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/reprlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/getpagesize Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/getrlimit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/getrusage Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/setrlimit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/resource/struct_rusage Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/rlcompleter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib