我使用的Windows 8.1 x64的Node.js的v0.10.31。 我注意到一个过程(一个Node.js的或Python脚本),处理SIGINT
处理程序,处理程序不是当信号从另一个Node.js的过程叫做发送process.kill(PID, "SIGINT")
和从而导致它终止。 不过我确实验证,如果SIGINT是按发送的处理程序被称为CTRL-C
控制台。
下面是处理Node.js的脚本SIGINT
(CoffeeScript的):
process.on 'SIGINT', -> console.log "SIGINT handled"
process.stdin.pipe(process.stdout)
console.log "PID: #{process.pid}"
控制台输出:
PID: 6916
SIGINT handled (this happens when pressing ctrl-c in console)
SIGINT handled (this happens when pressing ctrl-c in console)
# process terminates when another process calls process.kill(6916, 'SIGINT')
而这里的一个处理一个python脚本SIGINT
,这也是由node.js的无条件杀死process.kill(PID, 'SIGINT')
from signal import signal, SIGINT
import os
import time
def handler(signum, frame):
print "signal handled:", signum,
raise KeyboardInterrupt
signal(SIGINT, handler)
print "PID: ", os.getpid()
while True:
try:
time.sleep(1e6)
except KeyboardInterrupt:
print " KeyboardInterrupt handled"
控制台输出:
PID: 6440
signal handled:2 KeyboardInterrupt handled (this happens when pressing ctrl-c in console)
signal handled:2 KeyboardInterrupt handled (this happens when pressing ctrl-c in console)
# process terminated by another node.js script's process.kill(6440, 'SIGINT')
为什么不处理函数调用?