From ca23b5337e7747a7fbfdf3903f00a0ea3720a2f5 Mon Sep 17 00:00:00 2001 From: Ryan Dick Date: Thu, 20 Feb 2025 20:50:22 +0000 Subject: [PATCH] Simplify port selection logic to avoid the need for a global port variable. --- invokeai/app/api_app.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/invokeai/app/api_app.py b/invokeai/app/api_app.py index f1e72c8a66..d29356985b 100644 --- a/invokeai/app/api_app.py +++ b/invokeai/app/api_app.py @@ -60,10 +60,6 @@ logger.info(f"Using torch device: {torch_device_name}") loop = asyncio.new_event_loop() -# We may change the port if the default is in use, this global variable is used to store the port so that we can log -# the correct port when the server starts in the lifespan handler. -port = app_config.port - # Load custom nodes. This must be done after importing the Graph class, which itself imports all modules from the # invocations module. The ordering here is implicit, but important - we want to load custom nodes after all the # core nodes have been imported so that we can catch when a custom node clobbers a core node. @@ -77,7 +73,7 @@ async def lifespan(app: FastAPI): # Log the server address when it starts - in case the network log level is not high enough to see the startup log proto = "https" if app_config.ssl_certfile else "http" - msg = f"Invoke running on {proto}://{app_config.host}:{port} (Press CTRL+C to quit)" + msg = f"Invoke running on {proto}://{app_config.host}:{app_config.port} (Press CTRL+C to quit)" # Logging this way ignores the logger's log level and _always_ logs the message record = logger.makeRecord( @@ -198,17 +194,17 @@ def invoke_api() -> None: if app_config.dev_reload: enable_dev_reload() - global port - port = find_open_port(app_config.port) - if port != app_config.port: - logger.warn(f"Port {app_config.port} in use, using port {port}") + orig_config_port = app_config.port + app_config.port = find_open_port(app_config.port) + if orig_config_port != app_config.port: + logger.warning(f"Port {orig_config_port} is already in use. Using port {app_config.port}.") check_cudnn(logger) config = uvicorn.Config( app=app, host=app_config.host, - port=port, + port=app_config.port, loop="asyncio", log_level=app_config.log_level_network, ssl_certfile=app_config.ssl_certfile, @@ -223,7 +219,3 @@ def invoke_api() -> None: uvicorn_logger.addHandler(hdlr) loop.run_until_complete(server.serve()) - - -if __name__ == "__main__": - invoke_api()