像大多数语言一样,Python 也使用处理函数来处理系统信号。有关更多详细信息,请查看讨论接收和发送信号的信号章节,例如此处的示例。简而言之,您可以将一个函数绑定到一个或多个信号:>>> import signal>>> import sys>>> import time>>> >>> # Here we define a function that we want to get called.>>> def received_ctrl_c(signum, stack):... print("Received Ctrl-C")... sys.exit(0)... >>> # Bind the function to the standard system Ctrl-C signal.>>> handler = signal.signal(signal.SIGINT, received_ctrl_c)>>> handler<built-in function default_int_handler>>>> >>> # Now let’s loop forever, and break out only by pressing Ctrl-C, i.e. sending the SIGINT signal to the Python process.>>> while True:... print("Waiting…")... time.sleep(5)... Waiting…Waiting…Waiting…^CReceived Ctrl-C在您的特定情况下,找出机器人向您的 Python 进程(或任何侦听信号的进程)发送哪些信号,然后如上所示对它们采取行动。