# Python Resource Module

The `resource` module provides basic mechanisms for measuring and controlling system resources utilized by a program.

*Note: This module is only available on Unix.*

## Importing the Module

```python
import resource
```

## Resource Usage

You can retrieve resource usage information for the current process or its children using `resource.getrusage()`.

```python
import resource

usage = resource.getrusage(resource.RUSAGE_SELF)

print(f"User time: {usage.ru_utime}")
print(f"System time: {usage.ru_stime}")
print(f"Max RSS: {usage.ru_maxrss}")
```

## Resource Limits

The module allows you to get and set soft and hard limits on system resources.

*   **Soft Limit**: The current limit. Can be raised by the process up to the hard limit.
*   **Hard Limit**: The maximum value the soft limit can be raised to. Can only be lowered by the process (unless root).

### Getting Limits

```python
import resource

soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
print(f"Soft limit: {soft}, Hard limit: {hard}")
```

### Setting Limits

```python
import resource

# Set the maximum number of open files
resource.setrlimit(resource.RLIMIT_NOFILE, (1024, 2048))
```

## Common Resources

*   `resource.RLIMIT_CORE`: The maximum size (in bytes) of a core file that the current process can create.
*   `resource.RLIMIT_CPU`: The maximum amount of processor time (in seconds) that a process can use.
*   `resource.RLIMIT_FSIZE`: The maximum size of a file which the process may create.
*   `resource.RLIMIT_DATA`: The maximum size of the process's heap.
*   `resource.RLIMIT_STACK`: The maximum size of the call stack for the current process.
*   `resource.RLIMIT_RSS`: The maximum resident set size that should be made available to the process.
*   `resource.RLIMIT_NOFILE`: The maximum number of open file descriptors for the current process.
*   `resource.RLIMIT_AS`: The maximum area (in bytes) of address space which may be taken by the process.

[[programming/python/python]]