[英]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.