[英]Python trap routine
像大多数语言一样,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进程(或哪个进程侦听信号)的信号,然后对它们进行操作,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.