How to return the data in the child process generated by spawn to the node js main program in node js

for example, program:

< H1 > include < stdio.h > < / H1 > < H1 > include < unistd.h > < / H1 >

int main () {
int cntr = 0;
while (1) {
sleep (1);
cntrPP;
printf ("cntr =% dn", cntr);
}

After

}
is compiled into a getpsdata file,
then passes through:
var spawn = require ("child_process"). Spawn;
var ps = spawn (". / getpsdata", {stdio: ["pipe"," pipe", "pipe"]});

if (ps.stdout! = = null) {

ps.stdout.on( "data" , function(data) {
  console.log( "data" , data.toString());
});

} else {

console.log(ps.stdout);

}
/ / ps.stdout.on ("data", function (data) {
/ / console.log (" data", data.toString ());
/ /});

if (ps.stderr! = = null) {

ps.stderr.on( "data" , function(data) {
  console.log( "data" , data.toString());
});

} else {

console.log(ps.stderr);

}

/ / ps.stderr.on ("data", function (data) {
/ / console.log (" error", data.toString ());
/ /});

ps.on ("close", function (code) {
console.log (" close", code);
});

ps.on ("exit",function (code) {

)
console.log( "exit" , code);

});

ps.on ("error", function (code) {
console.log (" error", code);
});
I want to send the printed data in getpsdata to the main program of node. What should I do?

Jun.29,2021

I also encountered this problem, it is not untransferable, it still seems to write data to stdio, only at the end of the subroutine, the data event will be activated, and then you will be from

ps.stdout.on( 'data' , function(data) {
  console.log( 'data' , data.toString());
});

print out the data, and if the subroutine keeps running, the data event will not be activated.
I don't know why.

Menu