From 5b02b24f6c3669900e115e1cff78e90d7b33fccc Mon Sep 17 00:00:00 2001 From: Maeyanie Date: Tue, 21 May 2024 03:46:12 -0400 Subject: [PATCH] refactor: execute in a separate thread (#110) Just running async was still blocking the WebUI while a generation was running. --- scripts/script.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scripts/script.py b/scripts/script.py index e995952..1f30ba5 100644 --- a/scripts/script.py +++ b/scripts/script.py @@ -2,8 +2,9 @@ from fastapi import FastAPI import gradio as gr -from gradio.utils import run_coro_in_background +import asyncio import requests +from threading import Thread from modules import scripts, script_callbacks, sd_models, shared @@ -15,8 +16,6 @@ def on_app_started(demo: Optional[gr.Blocks], app: FastAPI): - import asyncio - started = False @app.on_event("startup") @@ -24,7 +23,8 @@ def on_app_started(demo: Optional[gr.Blocks], app: FastAPI): async def startup_event(): nonlocal started if not started: - run_coro_in_background(horde.run) + thread = Thread(daemon=True, target=horde_thread) + thread.start() started = True # This is a hack to make sure the startup event is @@ -38,6 +38,10 @@ async def startup_event(): requests.get(f"{local_url}horde/startup-events") +def horde_thread(): + asyncio.run(horde.run()) + + def apply_stable_horde_settings( enable: bool, name: str,