Spaces:
Running
Running
Download lpu/runtime/background_worker.py from iparaco/Virtual-LPU: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/runtime/background_worker.py
- Command line
-
hf download hf://spaces/iparaco/Virtual-LPU/lpu/runtime/background_worker.py
-
curl -L -o background_worker.py https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/runtime/background_worker.py
1.78 kB
| """Background worker: status command helper for `lpu status`. | |
| Reads controller status.json + active sessions + last discovery snapshot and | |
| returns a human/parse-friendly report. Kept separate from the controller so | |
| status queries never touch the controller loop. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import time | |
| from pathlib import Path | |
| from typing import Dict, List, Optional | |
| from ..monitoring.logger import lpu_home, read_jsonl | |
| from .lifecycle import find_controller_pid | |
| def controller_status() -> dict: | |
| home = lpu_home() | |
| pid = find_controller_pid() | |
| st_path = home / "status.json" | |
| status: Dict = { | |
| "controller_running": pid is not None, | |
| "controller_pid": pid, | |
| "ts": time.time(), | |
| } | |
| try: | |
| status["controller"] = json.loads(st_path.read_text(encoding="utf-8")) | |
| except (OSError, json.JSONDecodeError): | |
| status["controller"] = None | |
| status["note"] = "no status file yet (controller never started or crashed)" | |
| return status | |
| def active_sessions_report() -> List[dict]: | |
| from ..ipc import list_active_sessions | |
| return list_active_sessions(max_age_s=30.0) | |
| def last_gpu_samples(count: int = 5) -> List[dict]: | |
| home = lpu_home() | |
| path = home / "logs" / "controller_metrics.jsonl" | |
| if not path.exists(): | |
| return [] | |
| recs = [] | |
| for rec in read_jsonl(path): | |
| if isinstance(rec, dict) and rec.get("kind") == "gpu": | |
| recs.append(rec) | |
| return recs[-count:] | |
| def full_report() -> dict: | |
| return { | |
| "controller": controller_status(), | |
| "sessions": active_sessions_report(), | |
| "gpu_samples": last_gpu_samples(), | |
| } | |
| if __name__ == "__main__": # pragma: no cover | |
| print(json.dumps(full_report(), indent=2, default=str)) | |