From a6dcfc59b3fa5f16b207f3cf38aee5d8f18a08e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Thu, 7 Nov 2024 14:18:21 +0100 Subject: [PATCH] fix: add SIGINT handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Setup a SIGINT handler to gracefully exit the program once the user presses ctrl+c. Signed-off-by: Sébastien Han --- torchchat.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/torchchat.py b/torchchat.py index 35cdcabae..1eeee0120 100644 --- a/torchchat.py +++ b/torchchat.py @@ -6,7 +6,7 @@ import argparse import logging -import subprocess +import signal import sys # MPS ops missing with Multimodal torchtune @@ -25,7 +25,15 @@ default_device = "cpu" +def signal_handler(sig, frame): + print("\nInterrupted by user. Bye!\n") + sys.exit(0) + + if __name__ == "__main__": + # Set the signal handler for SIGINT + signal.signal(signal.SIGINT, signal_handler) + # Initialize the top-level parser parser = argparse.ArgumentParser( prog="torchchat",