所以我有這樣的代碼(從Python文檔部分採取):Python signal.signal是否阻止傳播?
import signal
def handler(signum, frame):
print 'Signal handler called with signal', signum
s = signal.signal(signal.SIGINT, handler)
some_fancy_code() # this code is using subprocess.Popen() to call another script
singal.signal(signal.SIGINT, s)
我發現現在的問題是,如果我在我的程序做按Ctrl + C,它正確地進入該處理程序和打印。現在,我認爲在接收到Ctrl + C後,我的處理程序將禁止默認處理程序,例如我的子進程.Popen將不會獲得KeyboardInterrupt信號。但這種情況並非如此。
但是,當我們用'signal.SIG_IGN'替換'handler'時,這種傳播從不發生。修改後的代碼片段:
import signal
s = signal.signal(signal.SIGINT, signal.SIG_IGN)
some_fancy_code() # this code is using subprocess.Popen() to call another script
singal.signal(signal.SIGINT, s)
這是因爲SIG_IGN是用語言本身寫的某種'魔法'信號嗎?或者也許有辦法在我的處理程序中進行類似的壓制?
在閱讀了關於堆棧溢出的一些問題之後,我有點困惑。如果有人能夠向我澄清爲什麼這樣的行爲差異。