(device)
| 233 | |
| 234 | |
| 235 | def thread_render(device): |
| 236 | global current_state, current_state_error |
| 237 | |
| 238 | from easydiffusion import model_manager, runtime |
| 239 | from easydiffusion.backend_manager import backend |
| 240 | from requests import ConnectionError |
| 241 | |
| 242 | try: |
| 243 | runtime.init(device) |
| 244 | |
| 245 | weak_thread_data[threading.current_thread()] = { |
| 246 | "device": runtime.context.device, |
| 247 | "device_name": runtime.context.device_name, |
| 248 | "alive": True, |
| 249 | } |
| 250 | |
| 251 | current_state = ServerStates.LoadingModel |
| 252 | |
| 253 | while True: |
| 254 | try: |
| 255 | if backend.ping(timeout=1): |
| 256 | break |
| 257 | |
| 258 | time.sleep(1) |
| 259 | except (TimeoutError, ConnectionError): |
| 260 | time.sleep(1) |
| 261 | |
| 262 | model_manager.load_default_models(runtime.context) |
| 263 | current_state = ServerStates.Online |
| 264 | except Exception as e: |
| 265 | log.error(traceback.format_exc()) |
| 266 | weak_thread_data[threading.current_thread()] = {"error": e, "alive": False} |
| 267 | return |
| 268 | |
| 269 | while True: |
| 270 | session_cache.clean() |
| 271 | task_cache.clean() |
| 272 | if not weak_thread_data[threading.current_thread()]["alive"]: |
| 273 | log.info(f"Shutting down thread for device {runtime.context.device}") |
| 274 | model_manager.unload_all(runtime.context) |
| 275 | return |
| 276 | if isinstance(current_state_error, SystemExit): |
| 277 | current_state = ServerStates.Unavailable |
| 278 | return |
| 279 | task = thread_get_next_task() |
| 280 | if task is None: |
| 281 | idle_event.clear() |
| 282 | idle_event.wait(timeout=1) |
| 283 | continue |
| 284 | if task.error is not None: |
| 285 | log.error(task.error) |
| 286 | task.response = {"status": "failed", "detail": str(task.error)} |
| 287 | task.buffer_queue.put(json.dumps(task.response)) |
| 288 | continue |
| 289 | if current_state_error: |
| 290 | task.error = current_state_error |
| 291 | task.response = {"status": "failed", "detail": str(task.error)} |
| 292 | task.buffer_queue.put(json.dumps(task.response)) |
nothing calls this directly
no test coverage detected