mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(api): use next available port
Resolves #3515 @ebr @brandonrising can't imagine this would cause issues but just FYI
This commit is contained in:
parent
3db9a07eea
commit
c557402dbb
@ -4,6 +4,7 @@ import sys
|
|||||||
from inspect import signature
|
from inspect import signature
|
||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
import socket
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
@ -193,9 +194,20 @@ app.mount("/",
|
|||||||
)
|
)
|
||||||
|
|
||||||
def invoke_api():
|
def invoke_api():
|
||||||
|
def find_port(port: int):
|
||||||
|
"""Find a port not in use starting at given port"""
|
||||||
|
# Taken from https://waylonwalker.com/python-find-available-port/, thanks Waylon!
|
||||||
|
# https://github.com/WaylonWalker
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
if s.connect_ex(("localhost", port)) == 0:
|
||||||
|
return find_port(port=port + 1)
|
||||||
|
else:
|
||||||
|
return port
|
||||||
|
|
||||||
|
port = find_port(app_config.port)
|
||||||
# Start our own event loop for eventing usage
|
# Start our own event loop for eventing usage
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
config = uvicorn.Config(app=app, host=app_config.host, port=app_config.port, loop=loop)
|
config = uvicorn.Config(app=app, host=app_config.host, port=port, loop=loop)
|
||||||
# Use access_log to turn off logging
|
# Use access_log to turn off logging
|
||||||
server = uvicorn.Server(config)
|
server = uvicorn.Server(config)
|
||||||
loop.run_until_complete(server.serve())
|
loop.run_until_complete(server.serve())
|
||||||
|
Loading…
Reference in New Issue
Block a user