python信号处理教程_Python信号处理

Python信号处理详解
这篇教程介绍了Python中的信号处理,包括什么是信号、如何处理信号及Python信号模块的使用。通过示例展示了如何响应SIGINT信号,并解释了信号在不同操作系统中的差异和线程中的限制。

python信号处理教程

Welcome to python signal processing tutorial. This is going to be a very basic lesson on how to handle signals with python. A computer receives and transmits various commands with the help of signals. An operating system has got at least one signal listed for each type of command it is programmed to receive or generate. And now, we will look at the basic signal processing in python 3.

欢迎使用python信号处理教程。 这将是关于如何使用python处理信号的非常基础的课程。 计算机借助信号来接收和发送各种命令。 操作系统针对其编程要接收或生成的每种命令列出了至少一个信号。 现在,我们将介绍python 3中的基本信号处理。

什么是信号 (What is Signal)

A signal is like a notification of an event. When an event occurs in the system, a signal is generated to notify other programs about the event. For example, if you press Ctrl + c, a signal called SIGINT is generated and any program can actually be aware of that incident by simply reading that signal. Signals are identified by integers.

信号就像事件的通知。 当系统中发生事件时,会生成一个信号以通知其他程序有关该事件的信息。 例如,如果您按Ctrl + c ,则会生成一个称为SIGINT的信号,并且任何程序实际上都可以通过简单地读取该信号来了解该事件。 信号由整数标识。

Python信号处理 (Python signal processing)

Python signal module is required for almost all the basic signal handling operations in python.

python中几乎所有基本的信号处理操作都需要Python信号模块。

To understand how python signal processing works, we need to know about ‘signal handler’. Signal handler is a task or program, which is executed when a particular signal is detected. A handler takes two arguments, namely, the signal number and a frame.

要了解python信号处理的工作原理,我们需要了解“信号处理程序”。 信号处理程序是一项任务或程序,当检测到特定信号时执行。 处理程序采用两个参数,即信号编号和帧。

You may just print a line in the handler or initiate some other task in response to the signal. signal.signal() function assigns handlers to a signal. Now without further discussion, let’s proceed to an example.

您可以只在处理程序中打印一行,也可以响应该信号启动其他任务。 signal.signal()函数将处理程序分配给信号。 现在,无需进一步讨论,我们来看一个示例。

Python信号示例 (Python Signal Example)

The following example shows how to program the system to respond to a SIGINT signal.

以下示例显示了如何对系统进行编程以响应SIGINT信号。

import signal  
import time  
  
  
def handler(a, b):  # define the handler  
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler)  # assign the handler to the signal SIGINT  
  
while 1:  
    print("Press ctrl + c")  # wait for SIGINT  
    time.sleep(10)

Lines 1 and 2 imports the signal and time module to be used in the program.

第1行和第2行导入要在程序中使用的信号和时间模块

Signal handler is defined in lines 5 to 6. It prints the integer value of the signal and the frame it receives along with the signal.

信号处理程序在第5至6行中定义。它输出信号的整数值以及与信号一起接收的帧。

Line 8 uses signal.signal() function to assign the handler to the signal SIGINT. This means every time the CPU picks up a ‘ctrl + c’ signal, the function handler is called.

第8行使用signal.signal()函数将处理程序分配给信号SIGINT。 这意味着每次CPU拾取“ ctrl + c”信号时,都会调用函数处理程序。

Line 10 to 12 is written to keep the program running indefinitely.

编写第10至12行以使程序无限期运行。

To execute this program, save the code as python_signal.py and open command window at the same folder. Then run the command python python_signal.py in the command window. The program should be running by then. Now press ctrl + c to obtain the following result. You may terminate the program by simply closing the command window or pressing ctrl + z.

要执行此程序,请将代码另存为python_signal.py并在同一文件夹中打开命令窗口。 然后在命令窗口中运行命令python python_signal.py 。 该程序应在那时运行。 现在按ctrl + c获得以下结果。 您可以通过简单地关闭命令窗口或按ctrl + z来终止程序。

Below image shows the output produced by above python signal processing example.

下图显示了以上python信号处理示例产生的输出。

Python信号警报 (Python Signal Alarm)

Let’s see another example to demonstrate the use of SIGALARM signal.

让我们看另一个示例来演示SIGALARM信号的用法。

import signal  
import time  
  
  
def alarm_handler(signum, stack):  
    print('Alarm at:', time.ctime())  
  
  
signal.signal(signal.SIGALRM, alarm_handler)  # assign alarm_handler to SIGALARM  
signal.alarm(4)  # set alarm after 4 seconds  
print('Current time:', time.ctime())  
time.sleep(6)  # make sufficient delay for the alarm to happen

Above python alarm signal program produces following output.

上面的python警报信号程序产生以下输出。

In this example, it is notable that a delay is made at line 12 so that the program does not terminate before the alarm time. The alarm is set after 4 seconds (Line 10) and alarm_handler does what the alarm has to do.

在此示例中,值得注意的是,在第12行进行了延迟,以使程序不会在警报时间之前终止。 警报在4秒后设置(第10行),并且alarm_handler执行警报必须执行的操作。

Python信号处理摘要 (Python Signal Processing Summary)

It has to be remembered that signals are not same for every operating systems. Some of the signals work in all the operating systems while others don’t. And while working with threads, only the main thread of a process can receive signals. So that’s it for python signal processing. Hope to come up with more tutorials very soon.

必须记住,每个操作系统的信号都不相同。 有些信号在所有操作系统中都有效,而另一些则不能。 在使用线程时,仅进程的主线程可以接收信号。 这就是python信号处理的过程。 希望很快能有更多的教程。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/16039/python-signal-processing

python信号处理教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值