@@ -21,6 +21,7 @@ def __init__(self, max_workers: int = 1, **kwargs):
2121 self .cleanup_lock = asyncio .Lock ()
2222
2323 async def set_prompts (self , prompts : List [PromptDictInput ]):
24+ await self .cancel_running_prompts ()
2425 self .current_prompts = [convert_prompt (prompt ) for prompt in prompts ]
2526 for idx in range (len (self .current_prompts )):
2627 task = asyncio .create_task (self .run_prompt (idx ))
@@ -32,38 +33,49 @@ async def update_prompts(self, prompts: List[PromptDictInput]):
3233 raise ValueError (
3334 "Number of updated prompts must match the number of currently running prompts."
3435 )
35- self .current_prompts = [convert_prompt (prompt ) for prompt in prompts ]
36+ # Validation step before updating the prompt, only meant for a single prompt for now
37+ for idx , prompt in enumerate (prompts ):
38+ converted_prompt = convert_prompt (prompt )
39+ try :
40+ await self .comfy_client .queue_prompt (converted_prompt )
41+ self .current_prompts [idx ] = converted_prompt
42+ except Exception as e :
43+ raise Exception ("Prompt update failed" ) from e
3644
3745 async def run_prompt (self , prompt_index : int ):
3846 while True :
3947 try :
4048 await self .comfy_client .queue_prompt (self .current_prompts [prompt_index ])
49+ except asyncio .CancelledError :
50+ raise
4151 except Exception as e :
4252 await self .cleanup ()
4353 logger .error (f"Error running prompt: { str (e )} " )
4454 raise
4555
4656 async def cleanup (self ):
57+ await self .cancel_running_prompts ()
4758 async with self .cleanup_lock :
48- tasks_to_cancel = list (self .running_prompts .values ())
49- for task in tasks_to_cancel :
50- task .cancel ()
51- try :
52- await task
53- except asyncio .CancelledError :
54- pass
55- self .running_prompts .clear ()
56-
5759 if self .comfy_client .is_running :
5860 try :
5961 await self .comfy_client .__aexit__ ()
6062 except Exception as e :
6163 logger .error (f"Error during ComfyClient cleanup: { e } " )
6264
63-
6465 await self .cleanup_queues ()
6566 logger .info ("Client cleanup complete" )
6667
68+ async def cancel_running_prompts (self ):
69+ async with self .cleanup_lock :
70+ tasks_to_cancel = list (self .running_prompts .values ())
71+ for task in tasks_to_cancel :
72+ task .cancel ()
73+ try :
74+ await task
75+ except asyncio .CancelledError :
76+ pass
77+ self .running_prompts .clear ()
78+
6779
6880 async def cleanup_queues (self ):
6981 while not tensor_cache .image_inputs .empty ():
0 commit comments