首頁  >  文章  >  後端開發  >  如何在Python中捕捉SIGINT訊號?

如何在Python中捕捉SIGINT訊號?

PHPz
PHPz轉載
2023-09-17 10:29:02970瀏覽

如何在Python中捕捉SIGINT訊號?

在本文中,我們將學習如何在 Python 中捕獲 SIGINT 以及捕獲後需要做什麼。

訊號模組接收到訊號後,會執行一定的動作。除此之外,它還可以使用SIGINT捕捉使用者通過鍵盤的中斷。

所需模組

訊號模組

術語「訊號」是指程式可以從作業系統接收訊息的過程。此外,當作業系統偵測到特定事件時,訊號會傳送到程式。透過在終端機執行以下命令,可以安裝訊號模組 -

pip install signal

系統模組

Python 中的 sys 模組提供了多個函數和變數來更改 Python 運行環境的不同部分。 sys 模組可以使用下列指令安裝 -

pip install os-sys

時間模組

Python 的時間模組使用戶能夠處理時間並記錄有關時間的資訊。 time模組通常是Python預先安裝的,所以不需要安裝;但是,如果沒有,您可以使用下面的命令來安裝它 -

pip install python-time

下面我們一步一步介紹一下Python中捕獲SIGINT的實作。

逐步實作

第 1 步:導入庫

首先,我們必須使用import關鍵字匯入所有必要的函式庫。 signal、sys 和 sleep 函式庫都在其中。

# importing signal and sys modules 
import signal
import sys
# importing sleep function from the time module
from time import sleep

第 2 步:建立函數

我們現在建立一個函數,在鍵盤中斷的情況下,將透過接受任何兩個參數來呼叫該函數。在本例中,參數被視為 sigframe

# creating a function that accepts the two arguments 
# it is called when the user makes a keyboard interruption
def signalHandling(signal, frame):

第 3 步:定義自訂處理程序

這裡我們使用 signal.signal() 函數來定義接收到訊號時必須呼叫的自訂處理程序。此外,我們定義了 signal.SIGINT,它透過在鍵盤上鍵入 Ctrl CCtrl F2 來引起中斷。

signal.signal(signal.SIGINT, signalHandling)

第4步:列印隨機訊息

接下來,列印任幾行隨機訊息,讓使用者知道如果鍵盤中斷該怎麼辦。

# printing random message
print(' printing random messages')

第 5 步:設定睡眠時間

最後,將 Python 睡眠時間設定為隨機的秒數。

# sleep time 
sleep(time in sec)

注意

該程式有一個問題:如果您在Windows 上運行它,您可以透過按Ctrl 和F2 來停止它並捕獲SIGINT,但如果您在Linux 上運行它,您可以停止它同時按Ctrl 和C

在Python中捕獲SIGINT

演算法(步驟)

以下是執行所需任務所需遵循的演算法/步驟。 -

  • 使用 import 關鍵字導入 signalsys 模組。

  • 使用 import 關鍵字從時間模組匯入 sleep 函數。

  • 建立一個變量,並將其值初始化為1(用於表示循環執行的次數)。

  • 使用while True,無限次循環。

  • 使用 try- except 區塊來處理錯誤/異常。

  • 透過列印上述變數來列印循環執行的次數。

  • 使用 sleep() 函數透過將數字作為參數傳遞給它,在列印每個數字之間休眠隨機秒數的時間。

  • 將迴圈執行計數值加 1。

  • 使用 except區塊來處理鍵盤中斷異常。

  • 如果發生鍵盤中斷異常,則列印任何訊息。

  • 使用sys模組的exit()函數關閉/退出程式。

範例

以下程式使用 try/catch 異常捕獲 SIGINT -

# importing signal and sys modules
import signal
import sys
# importing sleep function from the time module
from time import sleep
# initializing variable with value 1 to count the number of times the loop is executed
k = 1
# looping infinite times using a while loop
while True:
   # using try-except blocks for handling errors/exceptions
   try:
      # Printing the count of the number of times the loop is executed
      print(k)
      #sleeping for a random number of seconds time between printing of a number
      sleep(0.5)
      # Incrementing the loop execution count by 1
      k += 1
   # Handling the keyboard interruption exception using except block
   except KeyboardInterrupt:
      # printing any message if keyboard interruption exception occurs
      print("The loop has stopped!!")
      # closing/exiting the program
      sys.exit()

輸出

執行時,上述程式將產生以下輸出 -

1
2
3
4
5
6
7
8
9
10
11
12
13
The loop has stopped!!

在這個程式中,我們使用try-catch語句來處理鍵盤異常。在 try 區塊中執行數字遞增循環時,我們在 catch 區塊中捕獲了鍵盤中斷。

結論

在本文中,我們學習如何使用Python捕獲SIGINT。我們學習如何使用 try/catch 語句來實現相同的目的。 Try和catch語句可用於處理除0、鍵盤中斷等異常。

以上是如何在Python中捕捉SIGINT訊號?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除