From b93eb689fa8827abd1cc6f9e14b62dad7cff6b9d Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Sun, 13 Nov 2022 14:09:56 -0700 Subject: [PATCH] Do cpu & gpu work asynchonously --- frigate/stats.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/frigate/stats.py b/frigate/stats.py index 7ed1aa914..7ee400332 100644 --- a/frigate/stats.py +++ b/frigate/stats.py @@ -1,3 +1,4 @@ +import asyncio import json import logging import threading @@ -83,7 +84,15 @@ def get_temperatures() -> dict[str, float]: return temps -def get_gpu_stats(config: FrigateConfig) -> Optional[dict[str, dict]]: +def set_cpu_stats(all_stats: dict[str, str]) -> None: + """Set cpu usage from top.""" + cpu_stats = get_cpu_stats() + + if cpu_stats: + all_stats["cpu_usages"] = cpu_stats + + +def set_gpu_stats(config: FrigateConfig, all_stats: dict[str, str]) -> None: """Parse GPUs from hwaccel args and use for stats.""" hwaccel_args = [] @@ -136,9 +145,7 @@ def get_gpu_stats(config: FrigateConfig) -> Optional[dict[str, dict]]: pass if stats: - return stats - - return None + all_stats["gpu_usages"] = stats def stats_snapshot( @@ -181,8 +188,10 @@ def stats_snapshot( } stats["detection_fps"] = round(total_detection_fps, 2) - stats["cpu_usages"] = get_cpu_stats() - stats["gpu_usages"] = get_gpu_stats(config) + asyncio.gather( + asyncio.create_task(set_gpu_stats(config, stats)), + asyncio.create_task(set_cpu_stats(stats)), + ) stats["service"] = { "uptime": (int(time.time()) - stats_tracking["started"]),