From 32ac1ea1b6742ea27c482b3676599b657118b1eb Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 12 Aug 2022 21:02:18 +0300 Subject: [PATCH] Allow user to quit application from the terminal via SIGINT Call python interpreter at regular interval to handle any interrupt signals. create custom handler to terminate server and application --- src/main.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index bace607f..404e244c 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,5 @@ # Standard Packages +import signal import sys # External Packages @@ -6,7 +7,7 @@ import uvicorn from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from PyQt6 import QtWidgets -from PyQt6.QtCore import QThread +from PyQt6.QtCore import QThread, QTimer # Internal Packages from src.configure import configure_server @@ -49,12 +50,24 @@ def run(): if args.config is None: configure_screen.show() + # Setup Signal Handlers + signal.signal(signal.SIGINT, sigint_handler) + # Invoke python Interpreter every 500ms to handle signals + timer = QTimer() + timer.start(500) + timer.timeout.connect(lambda: None) + # Start Application server.start() gui.aboutToQuit.connect(server.terminate) gui.exec() +def sigint_handler(*args): + print("\nShutting down Khoj...") + QtWidgets.QApplication.quit() + + def set_state(args): state.config_file = args.config_file state.config = args.config