local-llm/comfyui/idle-unload.py
Philip Peterson 84e8326e61 ComfyUI with Qwen-Image and SDXL; Ollama keep-alive 30m
ComfyUI serves image generation to Open WebUI on the Tart VM
(ai.quineglobal.com). Models unload when idle on both sides so they
share the 64 GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 09:52:07 -08:00

41 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Unload ComfyUI's models once it has been idle for IDLE_MINUTES.
ComfyUI keeps models in memory until something else needs the room, and on
this 64 GB Mac that something is Ollama. Run every few minutes by
com.comfyui.idle-unload.plist; the counterpart of OLLAMA_KEEP_ALIVE=30m.
"""
import json
import time
import urllib.request
BASE = "http://127.0.0.1:8188"
IDLE_MINUTES = 15
def get(path):
with urllib.request.urlopen(BASE + path, timeout=10) as r:
return json.load(r)
try:
queue = get("/queue")
except OSError:
raise SystemExit(0) # ComfyUI not running
if queue["queue_running"] or queue["queue_pending"]:
raise SystemExit(0)
# Newest finish time (ms) of any job in the history.
last = 0
for job in get("/history?max_items=20").values():
for kind, data in job["status"].get("messages", []):
last = max(last, data.get("timestamp", 0))
if time.time() - last / 1000 < IDLE_MINUTES * 60:
raise SystemExit(0)
req = urllib.request.Request(
BASE + "/free",
data=json.dumps({"unload_models": True, "free_memory": True}).encode(),
headers={"Content-Type": "application/json"},
)
urllib.request.urlopen(req, timeout=10)