INNER CODE UNIT · Python
signal_handler
pairinteraction/pairinteraction · src/pairinteraction_gui/app.py:47
def signal_handler(signal: int, frame: FrameType | None) -> None:
os.write(pipe_w, b"x") # Write a single byte to the pipe
signal.signal(signal.SIGINT, signal_handler)
def handle_signal() -> None:
os.read(pipe_r, 1) # Read the byte from the pipe to clear it
logger.info("Ctrl+C detected in terminal. Shutting down gracefully...")
self.quit()
sn = QSocketNotifier(pipe_r, QSocketNotifier.Type.Read, parent=self)
sn.activated.connect(handle_signal)
# Create a timer to ensure the event loop processes events regularly
# This makes Ctrl+C work even when the application is idle
timer = QTimer(self)
timer.timeout.connect(lambda: None) # Do nothing, just wake up the event loop
timer.start(200)