feat: add --unix-socket option for Unix socket listening

Add optional --unix-socket CLI flag that uses aiohttp.web.UnixSite
instead of TCPSite. When specified, --listen and --port are ignored.
TCP remains the default behavior when the flag is not provided.
This commit is contained in:
hanli 2026-02-28 12:01:59 +08:00
parent 94f1a1cc9d
commit 5e662fedc6
3 changed files with 32 additions and 8 deletions

View File

@ -37,6 +37,7 @@ parser = argparse.ArgumentParser()
parser.add_argument("--listen", type=str, default="127.0.0.1", metavar="IP", nargs="?", const="0.0.0.0,::", help="Specify the IP address to listen on (default: 127.0.0.1). You can give a list of ip addresses by separating them with a comma like: 127.2.2.2,127.3.3.3 If --listen is provided without an argument, it defaults to 0.0.0.0,:: (listens on all ipv4 and ipv6)")
parser.add_argument("--port", type=int, default=8188, help="Set the listen port.")
parser.add_argument("--unix-socket", type=str, default=None, help="Path to Unix socket file to listen on. When specified, --listen and --port are ignored.")
parser.add_argument("--tls-keyfile", type=str, help="Path to TLS (SSL) key file. Enables TLS, makes app accessible at https://... requires --tls-certfile to function")
parser.add_argument("--tls-certfile", type=str, help="Path to TLS (SSL) certificate file. Enables TLS, makes app accessible at https://... requires --tls-keyfile to function")
parser.add_argument("--enable-cors-header", type=str, default=None, metavar="ORIGIN", nargs="?", const="*", help="Enable CORS (Cross-Origin Resource Sharing) with optional origin or allow all with default '*'.")

18
main.py
View File

@ -304,13 +304,15 @@ def prompt_worker(q, server_instance):
hook_breaker_ac10a0.restore_functions()
async def run(server_instance, address='', port=8188, verbose=True, call_on_start=None):
addresses = []
for addr in address.split(","):
addresses.append((addr, port))
await asyncio.gather(
server_instance.start_multi_address(addresses, call_on_start, verbose), server_instance.publish_loop()
)
async def run(server_instance, address='', port=8188, unix_socket=None, verbose=True, call_on_start=None):
if unix_socket:
start_coro = server_instance.start_unix_socket(unix_socket, verbose)
else:
addresses = []
for addr in address.split(","):
addresses.append((addr, port))
start_coro = server_instance.start_multi_address(addresses, call_on_start, verbose)
await asyncio.gather(start_coro, server_instance.publish_loop())
def hijack_progress(server_instance):
def hook(value, total, preview_image, prompt_id=None, node_id=None):
@ -419,7 +421,7 @@ def start_comfyui(asyncio_loop=None):
async def start_all():
await prompt_server.setup()
await run(prompt_server, address=args.listen, port=args.port, verbose=not args.dont_print_server, call_on_start=call_on_start)
await run(prompt_server, address=args.listen, port=args.port, unix_socket=args.unix_socket, verbose=not args.dont_print_server, call_on_start=call_on_start)
# Returning these so that other code can integrate with the ComfyUI loop and server
return asyncio_loop, prompt_server, start_all

View File

@ -1219,6 +1219,27 @@ class PromptServer():
if call_on_start is not None:
call_on_start(scheme, self.address, self.port)
async def start_unix_socket(self, unix_socket, verbose=True):
if sys.platform == 'win32':
raise RuntimeError("Unix sockets are not supported on Windows. Please use --listen and --port instead.")
runner = web.AppRunner(self.app, access_log=None)
await runner.setup()
if verbose:
logging.info("Starting server\n")
if os.path.exists(unix_socket):
os.unlink(unix_socket)
site = web.UnixSite(runner, unix_socket)
await site.start()
os.chmod(unix_socket, 0o666)
self.address = unix_socket
self.port = None
self.unix_socket = unix_socket
if verbose:
logging.info("Listening on Unix socket: {}".format(unix_socket))
def add_on_prompt_handler(self, handler):
self.on_prompt_handlers.append(handler)