@@ -371,6 +371,7 @@ async def launch_and_monitor(cmd, listen, port):
371
371
372
372
# NOTE: To prevent encoding error on Windows platform
373
373
env = dict (os .environ , PYTHONIOENCODING = "utf-8" )
374
+ env ["COMFY_CLI_BACKGROUND" ] = "true"
374
375
375
376
if sys .platform == "win32" :
376
377
process = subprocess .Popen (
@@ -399,7 +400,6 @@ def msg_hook(stream):
399
400
400
401
while True :
401
402
line = stream .readline ()
402
-
403
403
if "Launching ComfyUI from:" in line :
404
404
logging_flag = True
405
405
elif "To see the GUI go to:" in line :
@@ -492,25 +492,81 @@ def launch_comfyui(extra):
492
492
ConfigManager ().get_config_path (), "tmp" , str (uuid .uuid4 ())
493
493
)
494
494
new_env ["__COMFY_CLI_SESSION__" ] = session_path
495
+ new_env ["PYTHONENCODING" ] = "utf-8"
495
496
496
497
# To minimize the possibility of leaving residue in the tmp directory, use files instead of directories.
497
498
reboot_path = os .path .join (session_path + ".reboot" )
498
499
499
500
extra = extra if extra is not None else []
500
501
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
+ )
505
510
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 )
509
514
510
- if not os .path .exists (reboot_path ):
511
- exit (res )
515
+ if not os .path .exists (reboot_path ):
516
+ exit (res )
512
517
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 )
514
570
515
571
516
572
@app .command (help = "Stop background ComfyUI" )
0 commit comments