我有兩個Python腳本foo.py
和bar.py
,foo.py
將通過os.system()
調用bar.py
。在Python中如何處理信號和KeyboardInterrupt?
#foo.py
import os
print os.getpid()
os.system("python dir/bar.py")
#bar.py
import time
time.sleep(10)
print "over"
說的foo.py
的PID是123,如果程序正常終止,它會打印
123
over
如果鍵入kill 123
,而它的運行,我去拿以下輸出
123
Terminated
over
如果我按按Ctrl - C,而它的運行,我就會得到這樣
123
^CTraceback (most recent call last):
File "dir/bar.py", line 4, in <module>
time.sleep(10)
KeyboardInterrupt
但如果我鍵入kill -SIGINT 123
,而它的運行,似乎該程序會忽略的信號,並正常退出。
123
over
在我看來是,
如果我鍵入kill 123
,子進程將不會受到影響。
如果我輸入Ctrl - C,兩個過程都將被終止。
如果在子進程運行時鍵入kill -SIGINT 123
,則信號將被忽略。
有人請向我解釋它是如何工作的?
是不是Ctrl - C和kill -SIGINT
應該是等效的嗎?
如果我輸入kill 123
是否保證子過程不會受到影響(如果碰巧正在運行)?
我在Ubuntu 14.04上的方式。謝謝!