A Python utility for monitoring and managing system resources. It helps prevent system overload by monitoring CPU, GPU, memory, and network usage while providing an API for resource status and pruning decisions.
- Real-time Resource Monitoring: Track CPU, GPU, memory, and network usage in real-time.
- Adaptive Resource Management: Define thresholds for resource usage and get pruning signals when they're exceeded.
- Asynchronous Operation: All operations can be performed asynchronously.
- FastAPI Integration: Built-in REST API for remote monitoring and management.
- Simple Integration: Designed to be easily dropped into existing Python projects.
- Python 3.12+ and CUDA Compatible: Supports Python 3.12 and seamlessly integrates with CUDA environments when NVIDIA GPUs and
pynvml
are available. - Comprehensive Error Handling and Resilience: Includes proper error handling and resilience in API endpoints.
- Extensive Test Suite with High Coverage: Includes a test suite with high coverage to ensure reliability.
pip install git+https://github.com/saviornt/resourcemanager
from src.resource_manager import ResourceManager
# Initialize with custom thresholds
manager = ResourceManager(
max_cpu_percent=80.0,
max_memory_percent=75.0,
max_gpu_percent=90.0,
max_network_bytes_per_sec=1_000_000
)
# Check if pruning is needed
if manager.should_prune():
print("Pruning recommended due to high resource usage")
# Get individual metrics
cpu_usage = manager.resource_monitor.get_cpu_percent()
memory_usage = manager.resource_monitor.get_memory_usage_mb()
# Asynchronous usage
import asyncio
async def check_resources():
should_prune = await manager.should_prune_async()
if should_prune:
print("Async pruning recommended")
# Close when done
manager.close()
https://github.com/saviornt/ResourceManager
ResourceManager provides a FastAPI-based REST API for remote monitoring and resource management.
To start the API server:
import uvicorn
from resource_manager import endpoints
if __name__ == "__main__":
uvicorn.run(endpoints, host="0.0.0.0", port=8000)
The API provides the following endpoints:
Method: GET
Response Model: CPUResponse
Description: Returns current CPU usage percentage.
{
"cpu_percent": 25.7,
"timestamp": 1625482956.7891
}
Method: GET
Response Model: MemoryResponse
Description: Returns current memory usage in MB.
{
"memory_usage_mb": 4852.6,
"timestamp": 1625482956.7891
}
Method: GET
Response Model: GPUResponse
Description: Returns current GPU usage and memory usage percentages.
{
"gpu_percent": 42.3,
"gpu_memory_percent": 35.8,
"timestamp": 1625482956.7891
}
Method: GET
Response Model: NetworkResponse
Description: Returns current network usage in bytes per second.
{
"network_usage_bytes_per_second": 1250000,
"timestamp": 1625482956.7891
}
Method: GET
Response Model: AllResourcesResponse
Description: Returns all resource metrics in a single response.
{
"cpu_percent": 25.7,
"memory_usage_mb": 4852.6,
"gpu_percent": 42.3,
"gpu_memory_percent": 35.8,
"network_usage_bytes_per_second": 1250000,
"timestamp": 1625482956.7891
}
Method: GET
Response Model: PruneResponse
Description: Returns a boolean indicating whether resource pruning is necessary based on configured thresholds.
{
"should_prune": true,
"timestamp": 1625482956.7891
}
The API uses the following Pydantic models for responses:
CPUResponse
: CPU utilization dataMemoryResponse
: Memory usage dataGPUResponse
: GPU utilization and memory dataNetworkResponse
: Network usage dataAllResourcesResponse
: Combined resource dataPruneResponse
: Pruning decision data
- Caching: All responses are cached for 1 second to reduce system load
- Background Tasks: Cache is automatically refreshed in the background
- Error Handling: Comprehensive error handling with appropriate HTTP status codes
- Clean Shutdown: Resources are properly cleaned up when the API server shuts down