卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20455本站已运行348

如何在Python中捕获SIGINT信号?

如何在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、键盘中断等异常。

卓越飞翔博客
上一篇: 打印字符串中每个单词的最后一个字符
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏