本文介绍了Python-捕获所有信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Linux下的python 2.6中,我可以使用以下命令来处理TERM信号:
In python 2.6 under Linux, I can use the following to handle a TERM signal:
import signal
def handleSigTERM():
shutdown()
signal.signal(signal.SIGTERM, handleSigTERM)
除了一次设置一个信号以外,是否有任何方法可以为该过程接收到的所有信号设置处理程序?
Is there any way to setup a handler for all signals received by the process, other than just setting them up one-at-a-time?
推荐答案
您可以循环遍历信号模块中的信号并进行设置.
You could just loop through the signals in the signal module and set them up.
for i in [x for x in dir(signal) if x.startswith("SIG")]:
try:
signum = getattr(signal,i)
signal.signal(signum,sighandler)
except (OSError, RuntimeError) as m: #OSError for Python3, RuntimeError for 2
print ("Skipping {}".format(i))
这篇关于Python-捕获所有信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!