Node.js:无法处理从process.kill()发送的SIGINT [英] Node.js: SIGINT sent from process.kill() can't be handled

查看:263
本文介绍了Node.js:无法处理从process.kill()发送的SIGINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 8.1 x64上使用Node.js v0.10.31.我注意到,对于处理SIGINT处理程序的进程(node.js或python脚本),当process.kill(PID, "SIGINT")从另一个node.js进程发送信号时,不会调用该处理程序,从而导致其终止.但是我确实验证了如果在控制台中按CTRL-C键发送了SIGINT,则将调用处理程序.

I'm using Node.js v0.10.31 on Windows 8.1 x64. I noticed that for a process (a node.js or python script) that handles the SIGINT handler, the handler is not called when the signal is sent from another node.js process by process.kill(PID, "SIGINT"), and thus causing it to terminate. However I indeed verified that the handlers are called if the SIGINT is sent by pressing CTRL-C in console.

这是处理SIGINT(CoffeeScript)的Node.js脚本:

Here's the Node.js script that handles 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')

这是一个处理SIGINT的python脚本,该脚本也被node.js process.kill(PID, 'SIGINT')无条件杀死:

And here's a python script that handles SIGINT, which is also killed unconditionally by 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')

为什么不调用处理程序?

Why isn't the handler called?

推荐答案

似乎不是发送SIGINT的Node.js问题,而是Windows平台问题.那是因为当我从python程序发送SIGINT时,它也无条件地终止了处理SIGINT事件的进程:

It seems like it's not the problem of Node.js that sends SIGINT, but rather a Windows platform issue. That's because when I send SIGINT from a python program, it also unconditionally terminates the process that handles the SIGINT event:

os.kill(pid, signal.SIGINT)

幸运的是,Python记录得更好:

Luckily Python documents this better:

os.kill(pid,sig)

os.kill(pid, sig)

将信号sig发送到进程pid.的常数 主机平台上可用的特定信号在 信号模块.

Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.

Windows:signal.CTRL_C_EVENT和signal.CTRL_BREAK_EVENT信号 是特殊信号,只能发送到控制台进程 共享一个公共控制台窗口,例如一些子进程. 其他 sig的值将导致该过程无条件地被终止 ,然后将TerminateProcess API和退出代码设置为sig. Windows版本的kill()另外需要处理句柄 被杀死.

Windows: The signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT signals are special signals which can only be sent to console processes which share a common console window, e.g., some subprocesses. Any other value for sig will cause the process to be unconditionally killed by the TerminateProcess API, and the exit code will be set to sig. The Windows version of kill() additionally takes process handles to be killed.

这篇关于Node.js:无法处理从process.kill()发送的SIGINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆