Sending input to a child process in node.js

I am writing code to create an online C ++ compiler in node.js environment.Using spawnfunction I created a child process that will compile the code and execute it and send the weekend back to the user. But I need to send input to the current C ++ program. I used child.stdin.write('data'); to send data to a child, but I think that cinthe program does not receive input.
Please help me send input to C ++ executable code.
Thank.

+2
source share
2 answers

You should probably use either a cluster or fork if you want to send messages ... if you do, node will set IPC so you can communicate through process.send


Alternatively, you can use the pub / subsystem for communication channels (Redis works well for this), it is also the best choice if you need connections between servers.


Below is an example of an older script work ...

var env = process.env.NODE_ENV || 'dev';
var cluster = require("cluster");

//TODO: need to adjust to use domains for this work
process.on('uncaughtException', function (err) {
  console.error('GENERAL EXCEPTION IN %s: %s', process.env.WORKER_TYPE || 'MASTER',err);
  if (err.stack) console.error(err.stack);
  if (cluster.isWorker) {
    //process.send notifies the parent process of the error
    process.send({
      err: {
        "str": err && err.toString() || "unknown error"
        ,"message": err && err.message || null
        ,"stack": err && err.stack || null
      }
    });
  }
  process.nextTick(function(){
    process.exit(666);
  });
});


if (cluster.isMaster) startMaster();
if (cluster.isWorker) startWorker();

function startMaster() {
  createWorker("foo");
  createWorker("bar");
}

function createWorker(workerType) {
  var worker = cluster.fork({"WORKER_TYPE":workerType}); //passes environment variables to child
  worker.on('online',onOnline.bind(null, worker));
  worker.on('message',onMessage.bind(null, worker)); 
  worker.on('exit',onExit.bind(null, worker));
  worker.workerType = workerType;
  return worker; 
  // you can use worker.send() to send a message that 
  // will raise a message event in the child
}

function startWorker() {
  console.log("Running Worker: %s %s", cluster.worker.id, process.env.WORKER_TYPE);
  setTimeout(process.exit.bind(process,0), 5000); //close in 5 seconds
  //you may want to load a specific module based on WORKER_TYPE
}

function onOnline(worker) {
  console.log("Worker Online: %s %s", worker.id, worker.workerType);
  //console.log(arguments);
}

function onMessage(worker, msg) {
  if (msg.err) {
    console.warn("Error From", worker.id, worker.workerType, msg.err);
  } else {
    console.log("Message From", worker.id, worker.workerType);
  }
  //console.log(arguments);
}

function onExit(worker, code, signal) {
  console.log("Worker Exited: %s %s %s %s", worker.id, worker.workerType, code, signal);

    if (env == 'dev') {
        //for now just exit the whole thing (dev mode)
        process.nextTick(function(){
            process.exit(1);
        });
    } else {
        //workers should simply keep working...
        //fire off a new worker
        createWorker(worker.workerType);
    }

}
+2
source

1. Create a file with input data.
2. Create an input file ReadStream.
3. Press ReadStreamwith childprocess.stdin.

file=fs.createReadStream('input.txt',{encoding:'utf8'});
file.pipe(childprocess.stdin);

It worked for me.

+2
source

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


All Articles