CTRL-C可以由KeyboardInterrupt处理,我想知道如何处理CTRL-BREAK,你知道如何在python中做到这一点吗?
答案 0 :(得分:0)
如果您想忽略它,可以像这样add a signal handler:
signal.signal(signal.SIGBREAK, signal.SIG_IGN)
如果要正常关闭进程,则可以创建一个单独的信号处理程序函数,该函数将在收到信号时立即调用,如下所示:
import signal
def signal_handler(self, signum, frame):
print(f"Received {signum}! Shutting down...")
self.cleanup() # create your own clean up function
sys.exit()
if __name__ == "__main__":
signal.signal(signal.SIGBREAK, signal_handler)
# do stuff here in main