简体   繁体   English

NodeJS Fork无法杀死子进程

[英]NodeJS Fork can't get childprocess to kill

I'm running against a wall here, maybe it's just a small problem where I can't see the solution due to my inexperience with NodeJS. 我在这里碰壁,也许这只是一个小问题,由于我对NodeJS的缺乏经验,我看不到解决方案。

Right now I'm constructing a BT device which will be controlled by a master application and I have settled for the prototyping on a Raspberry PI 3 with NodeJS using the Bleno module. 现在,我正在构建一个将由主应用程序控制的BT设备,并且已经准备好使用Bleno模块使用NodeJS在Raspberry PI 3上进行原型设计。

So far everything worked fine, the device gets found and I can set and get values over Bluetooth. 到目前为止,一切正常,找到了设备,我可以通过蓝牙进行设置和获取值。 But to separate the different "programs" which the device could execute from the Bluetooth logic (because of loops etc.) I have opted to extract these into external NodeJS files. 但是为了从蓝牙逻辑中分离出设备可以执行的不同“程序”(由于循环等),我选择将它们提取到外部NodeJS文件中。

The idea here was to use the NodeJS fork module and control the starting and stoppping of those processes through the main process. 这里的想法是使用NodeJS fork模块并通过主进程控制这些进程的启动和停止。

But herein my problems start. 但是这里我的问题开始了。 I can fork the different JavaScript files without problem and these get executed, but I can't get them to stop and I don't know how to fix it. 我可以派生不同的JavaScript文件而不会出现问题,这些文件将被执行,但是我无法停止它们,而且我不知道如何解决它。

Here's the code (simplified): 这是代码(简体):

var util = require('util');
var events = require('events');
var cp = require('child_process');
...
var ProgramTypeOne = {
NONE:    0,
ProgramOne: 1,
...
};
...
var currentProgram=null;
...

function BLEDevice() {
  events.EventEmitter.call(this);
  ...
  this.currentProgram=null;
  ...
  }

util.inherits(BLELamp, events.EventEmitter);

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram();
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

...
module.exports.currentProgram = currentProgram;
...
function killProgram(){
  if(this.currentProgram!=null){
          this.currentProgram.kill('SIGTERM');
      }
}

There seems to be a problem with the variable currentProgram which, seemingly, never gets the childprocess from the fork call. 变量currentProgram似乎存在问题,看来它从未从fork调用中获取子进程。 As I have never worked extensivley with JavaScript, except some small scripts on websites, I have no idea where exactly my error lies. 由于除了网站上的一些小脚本之外,我从未使用过JavaScript进行扩展,所以我不知道我的错误到底在哪里。 I think it has something to do with the handling of class variables. 我认为这与类变量的处理有关。

The starting point for me was the Pizza example of Bleno. 对我来说,出发点是比萨 Bleno的例子。

Hope the information is enough and that someone can help me out. 希望该信息足够,希望有人可以帮助我。
Thanks in advance! 提前致谢!

Since killProgram() is a standalone function outside of the scope of BLELamp , you need to call killProgram with the correct scope by binding BLELamp as this . 由于killProgram()BLELamp范围之外的独立函数,因此您需要通过绑定BLELamp this来调用具有正确范围的BLELamp Using apply ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ) should resolve it. 使用apply( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply )应该可以解决该问题。 The following I would expect would fix it (the only line change is the one invoking killProgram ): 我期望以下内容可以解决此问题(唯一的更改是调用killProgram ):

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram.apply(this);
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

As a side note, your code is kind of confusing because you have a standalone var currentProgram then a couple prototypes with their own bound this.currentProgram s. 附带说明一下,您的代码有点令人困惑,因为您有一个独立的var currentProgram然后是几个具有各自绑定this.currentProgram的原型。 I would change the names to prevent confusion. 我将更改名称以避免混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM