如何在Python中捕获SIGINT信号?

2023年 9月 17日 44.6k 0

如何在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 步:创建函数

我们现在创建一个函数,在键盘中断的情况下,将通过接受任意两个参数来调用该函数。在本例中,参数被视为 sig 和 frame。

# 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+C 或 Ctrl+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 关键字导入 signal 和 sys 模块。

  • 使用 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信号?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论