INNER CODE UNIT · Python
allow_ctrl_c
pairinteraction/pairinteraction · src/pairinteraction_gui/app.py:43
def allow_ctrl_c(self) -> None:
# Create a pipe to communicate between the signal handler and the Qt event loop
pipe_r, pipe_w = os.pipe()
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