Skip to content

Commit

Permalink
feat: comfyui stop (#432)
Browse files Browse the repository at this point in the history
* feat: comfyui stop

* fix: pipeline stop

* fix: nit

* fix: process restart

* fix: add todo
  • Loading branch information
varshith15 authored Feb 27, 2025
1 parent 657db85 commit 609ddc6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions runner/app/live/pipelines/comfyui.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,8 @@ def update_params(self, **params):
logging.info(f"ComfyUI Pipeline Prompt: {new_params.prompt}")
self.client.set_prompt(new_params.prompt)
self.params = new_params

#TODO: This is a hack to stop the ComfyStreamClient. Use the comfystream api to stop the client in 0.0.2
async def stop(self):
if self.client.comfy_client.is_running:
await self.client.comfy_client.__aexit__(None, None, None)
7 changes: 7 additions & 0 deletions runner/app/live/pipelines/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ def update_params(self, **params):
**params: Implementation-specific parameters
"""
pass

async def stop(self):
"""Stop the pipeline.
Called once when the pipeline is no longer needed.
"""
pass
15 changes: 14 additions & 1 deletion runner/app/live/streamer/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def __init__(self, pipeline_name: str):
self.start_time = 0.0

async def stop(self):
await asyncio.to_thread(self._stop_sync)
#TODO: syncrounous call might be blocking, figure out a better way
self._stop_sync()

def _stop_sync(self):
self.done.set()
time.sleep(2) # wait for the pipeline to stop

if not self.process.is_alive():
logging.info("Process already not alive")
return
Expand Down Expand Up @@ -100,6 +103,7 @@ def get_recent_logs(self, n=10) -> list[str]:

def process_loop(self):
self._setup_logging()
pipeline = None

def report_error(error_msg: str):
error_event = {
Expand Down Expand Up @@ -170,6 +174,15 @@ def report_error(error_msg: str):
report_error(f"Error processing frame: {e}")
except Exception as e:
report_error(f"Error in process run method: {e}")
finally:
self._cleanup_pipeline(pipeline)

def _cleanup_pipeline(self, pipeline):
if pipeline is not None:
try:
asyncio.get_event_loop().run_until_complete(pipeline.stop())
except Exception as e:
logging.error(f"Error stopping pipeline: {e}")

def _setup_logging(self):
level = (
Expand Down

0 comments on commit 609ddc6

Please sign in to comment.