Skip to content

Commit a271264

Browse files
authored
fix: pipe error from tqdm in launch --background (#79)
* fix: pipe error from tqdm in launch --background
1 parent aa8e3f3 commit a271264

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

comfy_cli/cmdline.py

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ async def launch_and_monitor(cmd, listen, port):
371371

372372
# NOTE: To prevent encoding error on Windows platform
373373
env = dict(os.environ, PYTHONIOENCODING="utf-8")
374+
env["COMFY_CLI_BACKGROUND"] = "true"
374375

375376
if sys.platform == "win32":
376377
process = subprocess.Popen(
@@ -399,7 +400,6 @@ def msg_hook(stream):
399400

400401
while True:
401402
line = stream.readline()
402-
403403
if "Launching ComfyUI from:" in line:
404404
logging_flag = True
405405
elif "To see the GUI go to:" in line:
@@ -492,25 +492,81 @@ def launch_comfyui(extra):
492492
ConfigManager().get_config_path(), "tmp", str(uuid.uuid4())
493493
)
494494
new_env["__COMFY_CLI_SESSION__"] = session_path
495+
new_env["PYTHONENCODING"] = "utf-8"
495496

496497
# To minimize the possibility of leaving residue in the tmp directory, use files instead of directories.
497498
reboot_path = os.path.join(session_path + ".reboot")
498499

499500
extra = extra if extra is not None else []
500501

501-
while True:
502-
res = subprocess.run(
503-
[sys.executable, "main.py"] + extra, env=new_env, check=False
504-
)
502+
process = None
503+
504+
if "COMFY_CLI_BACKGROUND" not in os.environ:
505+
# If not running in background mode, there's no need to use popen. This can prevent the issue of linefeeds occurring with tqdm.
506+
while True:
507+
res = subprocess.run(
508+
[sys.executable, "main.py"] + extra, env=new_env, check=False
509+
)
505510

506-
if reboot_path is None:
507-
print("[bold red]ComfyUI is not installed.[/bold red]\n")
508-
exit(res)
511+
if reboot_path is None:
512+
print("[bold red]ComfyUI is not installed.[/bold red]\n")
513+
exit(res)
509514

510-
if not os.path.exists(reboot_path):
511-
exit(res)
515+
if not os.path.exists(reboot_path):
516+
exit(res)
512517

513-
os.remove(reboot_path)
518+
os.remove(reboot_path)
519+
else:
520+
# If running in background mode without using a popen, broken pipe errors may occur when flushing stdout/stderr.
521+
def redirector_stderr():
522+
while True:
523+
if process is not None:
524+
print(process.stderr.readline(), end="")
525+
526+
def redirector_stdout():
527+
while True:
528+
if process is not None:
529+
print(process.stdout.readline(), end="")
530+
531+
threading.Thread(target=redirector_stderr).start()
532+
threading.Thread(target=redirector_stdout).start()
533+
534+
try:
535+
while True:
536+
if sys.platform == "win32":
537+
process = subprocess.Popen(
538+
[sys.executable, "main.py"] + extra,
539+
stdout=subprocess.PIPE,
540+
stderr=subprocess.PIPE,
541+
text=True,
542+
env=new_env,
543+
encoding="utf-8",
544+
shell=True, # win32 only
545+
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP, # win32 only
546+
)
547+
else:
548+
process = subprocess.Popen(
549+
[sys.executable, "main.py"] + extra,
550+
text=True,
551+
env=new_env,
552+
encoding="utf-8",
553+
stdout=subprocess.PIPE,
554+
stderr=subprocess.PIPE,
555+
)
556+
557+
process.wait()
558+
559+
if reboot_path is None:
560+
print("[bold red]ComfyUI is not installed.[/bold red]\n")
561+
os._exit(process.pid)
562+
563+
if not os.path.exists(reboot_path):
564+
os._exit(process.pid)
565+
566+
os.remove(reboot_path)
567+
except KeyboardInterrupt:
568+
if process is not None:
569+
os._exit(1)
514570

515571

516572
@app.command(help="Stop background ComfyUI")

0 commit comments

Comments
 (0)