10000 fix: pipe error from tqdm in launch --background by ltdrdata · Pull Request #79 · Comfy-Org/comfy-cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: pipe error from tqdm in launch --background #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 67 additions & 11 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
Loading
0