From bf1b6479223c4720eb53e3dde9afbf4a4f879f00 Mon Sep 17 00:00:00 2001 From: puke Date: Tue, 20 May 2025 09:40:04 +0800 Subject: [PATCH] Optimize submit logic --- oneapi.py | 56 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/oneapi.py b/oneapi.py index 2c6103f..dd417e5 100644 --- a/oneapi.py +++ b/oneapi.py @@ -50,10 +50,12 @@ async def execute_workflow(request): client_id = str(uuid.uuid4()) # Submit workflow to ComfyUI queue - prompt_id = await _queue_prompt(workflow, client_id) - - if not prompt_id: - return web.json_response({"error": "Failed to submit workflow"}, status=500) + try: + prompt_id = await _queue_prompt(workflow, client_id) + except Exception as e: + error_message = f"Failed to submit workflow: [{type(e)}] {str(e)}" + print(error_message) + return web.json_response({"error": error_message}, status=500) result = { "status": "queued", @@ -267,29 +269,29 @@ async def _upload_image(image_path) -> str: async def _queue_prompt(workflow, client_id): """Submit workflow to queue using HTTP API""" - try: - prompt_data = { - "prompt": workflow, - "client_id": client_id - } - - json_data = json.dumps(prompt_data) - - # Use aiohttp to send request - async with aiohttp.ClientSession() as session: - async with session.post("http://127.0.0.1:8188/prompt", - data=json_data, - headers={"Content-Type": "application/json"}) as response: - if response.status != 200: - print(f"Failed to submit workflow: {response.status}") - return None - - result = await response.json() - # Return prompt_id - return result.get("prompt_id") - except Exception as e: - print(f"Error submitting workflow: {str(e)}") - return None + prompt_data = { + "prompt": workflow, + "client_id": client_id + } + + json_data = json.dumps(prompt_data) + + # Use aiohttp to send request + async with aiohttp.ClientSession() as session: + async with session.post( + "http://127.0.0.1:8188/prompt", + data=json_data, + headers={"Content-Type": "application/json"} + ) as response: + if response.status != 200: + response_text = await response.text() + raise Exception(f"Failed to submit workflow: [{response.status}] {response_text}") + + result = await response.json() + prompt_id = result.get("prompt_id") + if not prompt_id: + raise Exception(f"Failed to get prompt_id: {result}") + return prompt_id async def _get_base_url(request): """