【问题标题】:Sending input data to child process in node.js在 node.js 中将输入数据发送到子进程
【发布时间】:2015-01-18 23:51:39
【问题描述】:

我正在编写代码以在 node.js 环境中创建一个在线 c++ 编译器。使用spawn 函数我创建了一个子进程,它将编译代码并执行它并将输出发送回用户。 但我需要将输入发送到正在运行的 c++ 程序。我用child.stdin.write('data'); 用于向孩子发送数据,但我认为程序中的cin 没有接收到输入。
请帮我将输入发送到正在运行的 c++ 代码。
谢谢。

【问题讨论】:

    标签: javascript c++ node.js online-compilation


    【解决方案1】:

    1.使用输入数据创建一个文件。
    2.创建ReadStream打开输入文件。
    3.将ReadStreamchildprocess.stdin连接起来。

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

    这对我有用。

    【讨论】:

      【解决方案2】:

      如果您想传递消息,您可能应该使用集群或分叉...如果您这样做,节点将为您设置 IPC,以便您能够通过 process.send 进行通信


      或者,您可以使用发布/订阅系统作为通信渠道(Redis 非常适合此),如果您需要跨服务器通信,这也是您的最佳选择。


      下面是一个较旧的工作脚本示例...

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

      【讨论】:

        猜你喜欢
        • 2012-11-06
        • 2012-07-26
        • 2012-12-29
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        • 2020-09-20
        • 2011-08-02
        相关资源
        最近更新 更多