signal
Table of Contents

signal

signal 包主要针对Unix平台

定义信号名

import signal
print(signal.SIGALRM)
print(signal.SIGCONT)
>>> 
14
19

预设信号处理函数

singnal.signal(signalnum, handler)
import signal

def myHandler(signum, frame):
    print('I received: ', signum)

signal.signal(signal.SIGTSTP, myHandler)
signal.pause()
print('End of Signal Demo')
>>>
CTRL+Z会可以发生SIGTSTP信号

定时发送SIGALARM 信号

import signal

def myHandler(signum, frame):
    print("Now, it's the time")
    exit()

signal.signal(signal.SIGALRM, myHandler)
signal.alarm(1)
while True:
    print('not yet')
>>>
not yet
not yet
not yet
Now, it's the time

发送信号

os.kill(pid, sid)
os.killpg(pgid, sid)