javascript - Sending input data to child process in node.js - Stack Overflow

I am writing code to create an online c++ piler in node.js environment.Using spawnfunction i created

I am writing code to create an online c++ piler in node.js environment.Using spawn function i created a child process which will pile the code and execute it and send the output back to the user. But i need to send input to the running c++ program . I used child.stdin.write('data'); for sending data to child but i think cin in the program is not receiving the input .
Please help me to send the input to the running c++ code.
Thanks.

I am writing code to create an online c++ piler in node.js environment.Using spawn function i created a child process which will pile the code and execute it and send the output back to the user. But i need to send input to the running c++ program . I used child.stdin.write('data'); for sending data to child but i think cin in the program is not receiving the input .
Please help me to send the input to the running c++ code.
Thanks.

Share Improve this question edited Nov 21, 2014 at 0:34 Sri Harsha asked Nov 20, 2014 at 22:50 Sri HarshaSri Harsha 6612 gold badges8 silver badges19 bronze badges 1
  • just in case someone is using IPC and JSON serialization, what I observed is that it sends message from Child-to-Parent but not Parent-to-Child using send() function. When I used fork instead of spawn, it started sending messages in both ways. – Ankur Thakur Commented Apr 27, 2023 at 14:06
Add a ment  | 

2 Answers 2

Reset to default 4

1.Create a file with input data.
2.Create a ReadStream opening input file.
3.Pipe the ReadStream with childprocess.stdin.

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

This worked for me .

You should probably use either cluster, or fork if you want to pass messages... if you do this, node will setup IPC for you to be able to municate via process.send


Alternatively, you could use a pub/sub system for munication channels (Redis works well for this), this is also your best bet if you need munications across servers.


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

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);
    }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744351903a4570027.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信