Overview
TheResourceLimits interface allows you to enforce limits on Python code execution to prevent resource exhaustion and ensure safe sandboxing of untrusted code.
All limits are optional. Omit a field to disable that specific limit.
Interface
Fields
Maximum number of heap allocations allowed during execution.When this limit is reached, execution terminates with a
ResourceError.Useful for preventing memory exhaustion from code that creates many objects.Example:Maximum execution time in seconds (floating point).When this time limit is exceeded, execution terminates with a
ResourceError.Useful for preventing infinite loops or long-running computations.Example:Maximum heap memory in bytes.When heap memory usage exceeds this limit, execution terminates with a
ResourceError.Useful for preventing memory exhaustion from large data structures.Example:Run garbage collection every N allocations.Controls how frequently the garbage collector runs. Lower values reduce peak memory usage but may slow execution. Higher values improve performance but may increase memory usage.If not specified, garbage collection is triggered automatically based on heap pressure.Example:
Maximum function call stack depth.Default:
1000When the call stack exceeds this depth, execution terminates with a RecursionError.Useful for preventing stack overflow from infinite recursion.Example:Usage Examples
Basic Resource Limiting
Preventing Infinite Recursion
Preventing Long-Running Code
Combining Multiple Limits
Using with runMontyAsync()
Best Practices
Recommended Limits for Untrusted Code
See Also
- Monty Class - Main interpreter class
- Errors - Error handling and exception types
