diff --git a/invokeai/app/api_app.py b/invokeai/app/api_app.py index 39644c4870..8b65bacb29 100644 --- a/invokeai/app/api_app.py +++ b/invokeai/app/api_app.py @@ -1,6 +1,9 @@ import asyncio import mimetypes +import os +import signal import socket +import sys from contextlib import asynccontextmanager from inspect import signature from pathlib import Path @@ -223,6 +226,27 @@ app.mount( def invoke_api() -> None: + class InterruptWatcher: + def __init__(self): + self.child = os.fork() + if self.child == 0: + return + else: + self.watch() + + def watch(self) -> None: + try: + os.wait() + except KeyboardInterrupt: + self.kill() + sys.exit() + + def kill(self) -> None: + try: + os.kill(self.child, signal.SIGKILL) + except OSError: + pass + def find_port(port: int) -> int: """Find a port not in use starting at given port""" # Taken from https://waylonwalker.com/python-find-available-port/, thanks Waylon! @@ -233,6 +257,8 @@ def invoke_api() -> None: else: return port + InterruptWatcher() + if app_config.dev_reload: try: import jurigged