diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index 4244d108..37247691 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -371,6 +371,7 @@ async def launch_and_monitor(cmd, listen, port): # NOTE: To prevent encoding error on Windows platform env = dict(os.environ, PYTHONIOENCODING="utf-8") + env["COMFY_CLI_BACKGROUND"] = "true" if sys.platform == "win32": process = subprocess.Popen( @@ -399,7 +400,6 @@ def msg_hook(stream): while True: line = stream.readline() - if "Launching ComfyUI from:" in line: logging_flag = True elif "To see the GUI go to:" in line: @@ -492,25 +492,81 @@ def launch_comfyui(extra): ConfigManager().get_config_path(), "tmp", str(uuid.uuid4()) ) new_env["__COMFY_CLI_SESSION__"] = session_path + new_env["PYTHONENCODING"] = "utf-8" # To minimize the possibility of leaving residue in the tmp directory, use files instead of directories. reboot_path = os.path.join(session_path + ".reboot") extra = extra if extra is not None else [] - while True: - res = subprocess.run( - [sys.executable, "main.py"] + extra, env=new_env, check=False - ) + process = None + + if "COMFY_CLI_BACKGROUND" not in os.environ: + # If not running in background mode, there's no need to use popen. This can prevent the issue of linefeeds occurring with tqdm. + while True: + res = subprocess.run( + [sys.executable, "main.py"] + extra, env=new_env, check=False + ) - if reboot_path is None: - print("[bold red]ComfyUI is not installed.[/bold red]\n") - exit(res) + if reboot_path is None: + print("[bold red]ComfyUI is not installed.[/bold red]\n") + exit(res) - if not os.path.exists(reboot_path): - exit(res) + if not os.path.exists(reboot_path): + exit(res) - os.remove(reboot_path) + os.remove(reboot_path) + else: + # If running in background mode without using a popen, broken pipe errors may occur when flushing stdout/stderr. + def redirector_stderr(): + while True: + if process is not None: + print(process.stderr.readline(), end="") + + def redirector_stdout(): + while True: + if process is not None: + print(process.stdout.readline(), end="") + + threading.Thread(target=redirector_stderr).start() + threading.Thread(target=redirector_stdout).start() + + try: + while True: + if sys.platform == "win32": + process = subprocess.Popen( + [sys.executable, "main.py"] + extra, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + env=new_env, + encoding="utf-8", + shell=True, # win32 only + creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, # win32 only + ) + else: + process = subprocess.Popen( + [sys.executable, "main.py"] + extra, + text=True, + env=new_env, + encoding="utf-8", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + process.wait() + + if reboot_path is None: + print("[bold red]ComfyUI is not installed.[/bold red]\n") + os._exit(process.pid) + + if not os.path.exists(reboot_path): + os._exit(process.pid) + + os.remove(reboot_path) + except KeyboardInterrupt: + if process is not None: + os._exit(1) @app.command(help="Stop background ComfyUI")