d3f05a19a5
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""Interrumpe la generacion en curso de ComfyUI y devuelve el estado de la cola.
|
|
|
|
Funcion impura: hace red (HTTP POST /interrupt + GET /queue). Solo stdlib.
|
|
|
|
POST /interrupt corta el prompt que ComfyUI esta ejecutando ahora mismo (no vacia
|
|
la cola: los prompts pendientes siguen). GET /queue devuelve queue_running (lo que
|
|
se ejecuta) y queue_pending (lo encolado). Esta funcion combina ambos en un dict
|
|
honesto que NO lanza excepcion en fallo de red: devuelve {ok: False, error}.
|
|
"""
|
|
import json
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
|
|
def comfyui_interrupt_queue(server: str = "127.0.0.1:8188") -> dict:
|
|
"""Interrumpe la generacion en curso y devuelve el estado de la cola.
|
|
|
|
Args:
|
|
server: host:port del servidor ComfyUI sin esquema (default
|
|
"127.0.0.1:8188").
|
|
|
|
Returns:
|
|
dict con:
|
|
- ok (bool): True si tanto el interrupt como la lectura de la cola
|
|
tuvieron exito.
|
|
- interrupted (bool): True si el POST /interrupt respondio sin error.
|
|
- queue_running (int): numero de prompts ejecutandose ahora mismo.
|
|
- queue_pending (int): numero de prompts encolados pendientes.
|
|
- error (str): mensaje de error si algo fallo; cadena vacia si todo OK.
|
|
"""
|
|
out = {
|
|
"ok": False,
|
|
"interrupted": False,
|
|
"queue_running": 0,
|
|
"queue_pending": 0,
|
|
"error": "",
|
|
}
|
|
base = f"http://{server}"
|
|
|
|
# 1. POST /interrupt (cuerpo vacio): corta el prompt en ejecucion.
|
|
try:
|
|
req = urllib.request.Request(f"{base}/interrupt", data=b"", method="POST")
|
|
with urllib.request.urlopen(req, timeout=10.0):
|
|
out["interrupted"] = True
|
|
except urllib.error.URLError as exc:
|
|
reason = getattr(exc, "reason", exc)
|
|
out["error"] = f"interrupt fallo: no se pudo conectar a {base}/interrupt: {reason}"
|
|
return out
|
|
|
|
# 2. GET /queue: estado actual de la cola tras el interrupt.
|
|
try:
|
|
with urllib.request.urlopen(f"{base}/queue", timeout=10.0) as resp:
|
|
data = json.loads(resp.read())
|
|
out["queue_running"] = len(data.get("queue_running", []))
|
|
out["queue_pending"] = len(data.get("queue_pending", []))
|
|
out["ok"] = True
|
|
except urllib.error.URLError as exc:
|
|
reason = getattr(exc, "reason", exc)
|
|
out["error"] = f"queue fallo: no se pudo conectar a {base}/queue: {reason}"
|
|
except json.JSONDecodeError as exc:
|
|
out["error"] = f"queue fallo: respuesta no es JSON valido: {exc}"
|
|
return out
|
|
|
|
|
|
if __name__ == "__main__":
|
|
res = comfyui_interrupt_queue()
|
|
print(
|
|
f"ok={res['ok']} interrupted={res['interrupted']} "
|
|
f"running={res['queue_running']} pending={res['queue_pending']} "
|
|
f"error={res['error']!r}"
|
|
)
|