diff --git a/ui/sd_internal/runtime.py b/ui/sd_internal/runtime.py index 34bb8c7a..9ed8404f 100644 --- a/ui/sd_internal/runtime.py +++ b/ui/sd_internal/runtime.py @@ -1,3 +1,9 @@ +"""runtime.py: torch device owned by a thread. +Notes: + Avoid device switching, transfering all models will get too complex. + To use a diffrent device signal the current render device to exit + And then start a new clean thread for the new device. +""" import json import os, re import traceback diff --git a/ui/sd_internal/task_manager.py b/ui/sd_internal/task_manager.py index 4bd1d549..10d66fd4 100644 --- a/ui/sd_internal/task_manager.py +++ b/ui/sd_internal/task_manager.py @@ -1,3 +1,9 @@ +"""task_manager.py: manage tasks dispatching and render threads. +Notes: + render_threads should be the only hard reference held by the manager to the threads. + Use weak_thread_data to store all other data using weak keys. + This will allow for garbage collection after the thread dies. +""" import json import traceback @@ -340,6 +346,11 @@ def start_render_thread(device='auto'): rthread.daemon = True rthread.name = THREAD_NAME_PREFIX + device rthread.start() + timeout = LOCK_TIMEOUT + while not rthread.is_alive(): + if timeout <= 0: raise Exception('render_thread', rthread.name, 'failed to start before timeout or has crashed.') + timeout -= 1 + time.sleep(1) render_threads.append(rthread) finally: manager_lock.release() diff --git a/ui/server.py b/ui/server.py index 68d22ac5..2d7f7bd8 100644 --- a/ui/server.py +++ b/ui/server.py @@ -1,3 +1,7 @@ +"""server.py: FastAPI SD-UI Web Host. +Notes: + async endpoints always run on the main thread. Without they run on the thread pool. +""" import json import traceback @@ -343,7 +347,7 @@ config = getConfig() async def check_status(): # Task to Validate user config shortly after startup. # Check that the loaded config.json yielded a server in a known valid state. - # Issues found, try to fix and warn the user. + # When issues are found, try to fix them when possible and warn the user. device_count = 0 for i in range(10): # Wait for devices to register and/or change names. await asyncio.sleep(3)