From c6947fffd9b6c906447ce36673e15848411e4b1b Mon Sep 17 00:00:00 2001 From: Giovanny Moreno Date: Thu, 18 Jun 2026 11:13:47 -0500 Subject: [PATCH] feat: make GPT-Image-2 nodes async for parallel execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ComfyUI's executor (execution.py) detects async node functions via inspect.iscoroutinefunction() and wraps them in asyncio.create_task() instead of awaiting sequentially. This allows multiple API nodes at the same graph depth to fire their HTTP calls simultaneously. Changes: - Add `import asyncio` to nodes.py - KIE_GPTImage2_TextToImage.generate: def → async def - KIE_GPTImage2_ImageToImage.generate: def → async def - Both now use asyncio.to_thread() to run the blocking HTTP job in a thread pool, allowing true concurrent I/O (GIL released for network) Result: N independent GPT-Image-2 nodes in a workflow run in parallel instead of sequentially. For 5 ad generation nodes this yields ~5x wall-clock speedup. KIE API supports 100+ concurrent tasks and 20 requests/10s, so the server side is not a bottleneck. No behaviour change for single-node workflows. --- nodes.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/nodes.py b/nodes.py index a15a390..e76561e 100644 --- a/nodes.py +++ b/nodes.py @@ -1,3 +1,4 @@ +import asyncio import json import os import time @@ -383,7 +384,7 @@ def INPUT_TYPES(cls): FUNCTION = "generate" CATEGORY = "kie/api" - def generate( + async def generate( self, prompt: str, aspect_ratio: str = "auto", @@ -395,7 +396,8 @@ def generate( max_retries: int = 2, retry_backoff_s: float = 3.0, ): - image_tensor = run_gpt_image2_text_to_image( + image_tensor = await asyncio.to_thread( + run_gpt_image2_text_to_image, prompt=prompt, aspect_ratio=aspect_ratio, resolution=resolution, @@ -451,7 +453,7 @@ def INPUT_TYPES(cls): FUNCTION = "generate" CATEGORY = "kie/api" - def generate( + async def generate( self, prompt: str, images: torch.Tensor, @@ -464,7 +466,8 @@ def generate( max_retries: int = 2, retry_backoff_s: float = 3.0, ): - image_tensor = run_gpt_image2_image_to_image( + image_tensor = await asyncio.to_thread( + run_gpt_image2_image_to_image, prompt=prompt, images=images, aspect_ratio=aspect_ratio,