Node.JS Forked Pipe

I am trying to unblock the node.js module as a child process in the example presented at https://stackoverflow.com/a/212964/169 . The plug itself works, but the problem I am facing is that the node is trying to add .on('data')and .on('exit')before it fork('./DaemonSerial.js'fillstChild

var fork = require('child_process').fork;

// Start Serial Daemon/s
var tChild = fork('./DaemonSerial.js', [], {
  stdio: 'pipe'
});
tChild.stdin.on('data', function(data) {
  // output from the child process
  console.log("./DaemonSerial.js >>> " + data)
 });
EdgeMaster.Children[tChild.pid] = tChild;
tChild.on('exit', function(d) {
    console.log("./DaemonSerial.js >>> "+ tChild.pid + ' Exited:'+ d);
    delete EdgeMaster.Children[tChild.pid]
 });

I also ran into this problem elsewhere, and I'm sure there must be a method to provide type functionality do THIS then THAT, even if the function itself does not have a callback. child_process.fork(modulePath, [args], [options])on nodejs.org/api/child_process.html does not display a callback.

ideas?

EDIT: script forktest.js, , script . forktest.js :

var fork = require('child_process').fork;

var ForkDict = {};

function forkit(aPath){
    tChild = fork( aPath, [], {stdio: 'pipe'});
    ForkDict[tChild.pid] = tChild;
    ForkDict[tChild.pid].path = aPath;
    tChild.stdout.on('data', function(data) {
        // output from the child process
        console.log( this.path +'>>> '+ data);
     }.bind(this));
    tChild.on('exit', function(d) {
        console.log( this.path +'>>> Exited:'+ d);
        delete ForkDict[tChild.pid]
     }.bind(this));
}

forkit('./DaemonSerial.js');

:

pi@raspberrypi ~ $ node forktest.js

/home/pi/forktest.js:9
    tChild.stdout.on('data', function(data) {
                  ^
TypeError: Cannot call method 'on' of null
    at forkit (/home/pi/forktest.js:9:19)
    at Object.<anonymous> (/home/pi/forktest.js:19:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
+4
1

fork , . ChildProcess, EventEmitter. EventEmitter , , - . docs:

( EventEmitter).

:

  • fork stdio. , silent: true; spawn stdio, "pipe".
  • (stdin): tChild.stdin.on('data', ... , stdout.

, stdin stdout null , silent: true . . options.silent fork:

Boolean true, stdin, stdout stderr , , . "pipe" "inherit" spawn() stdio ( false)

, script stdout. ( , stdio ):

tChild = fork( aPath, [], {silent: true});

, data stdout.

+9

Source: https://habr.com/ru/post/1530778/


All Articles