Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js子进程#生成绕过标准输入/标准输出内部缓冲区_Node.js_Buffer_Spawn - Fatal编程技术网

node.js子进程#生成绕过标准输入/标准输出内部缓冲区

node.js子进程#生成绕过标准输入/标准输出内部缓冲区,node.js,buffer,spawn,Node.js,Buffer,Spawn,我使用child#u process#spawn通过node.js使用外部二进制文件。根据语言,每个二进制搜索字符串中的精确单词,并根据输入文本生成输出。它们没有内部缓冲区。用法示例: echo“我是一个随机输入”|/我的英语二进制文件 生成的文本类似于句子中的单词X cat/dev/uradom |./my english binary产生无限输出 我想使用这些二进制文件中的每一个作为“服务器”。我想在遇到以前从未遇到过的语言后启动一个新的二进制实例,必要时使用stdin.write()向

我使用
child#u process#spawn
通过node.js使用外部二进制文件。根据语言,每个二进制搜索字符串中的精确单词,并根据输入文本生成输出。它们没有内部缓冲区。用法示例:

  • echo“我是一个随机输入”|/我的英语二进制文件
    生成的文本类似于句子中的单词X
  • cat/dev/uradom |./my english binary
    产生无限输出
我想使用这些二进制文件中的每一个作为“服务器”。我想在遇到以前从未遇到过的语言后启动一个新的二进制实例,必要时使用stdin.write()向其发送数据,并使用stdout.on('data')事件直接获取其输出。问题在于,在将大量数据发送到stdin.write()之前,不会调用stdout.on('data')。stdout或stdin(或两者)可能有内部阻塞缓冲区。。。但我希望尽快输出,因为否则,程序可能会在新输入出现之前等待数小时,然后解锁stdin.write()或stdout.on('data')。如何更改它们的内部缓冲区大小?或者我可以使用另一个非阻塞系统吗

我的代码是:

const spawn = require('child_process').spawn;
const path = require('path');

class Driver {

  constructor() {
    // I have one binary per language
    this.instances = {
      frFR: {
        instance: null,
        path: path.join(__dirname, './my-french-binary')
      },
      enGB: {
        instance: null,
        path: path.join(__dirname, './my-english-binary')
      }
    }
  };

  // this function just check if an instance is running for a language
  isRunning(lang) {
    if (this.instances[lang] === undefined)
      throw new Error("Language not supported by TreeTagger: " + lang);
    return this.instances[lang].instance !== null;
  }

  // launch a binary according to a language and attach the function 'onData' to the stdout.on('data') event
  run(lang, onData) {
    const instance = spawn(this.instances[lang].path,{cwd:__dirname});
    instance.stdout.on('data', buf => onData(buf.toString()));
    // if a binary instance is killed, it will be relaunched later
    instance.on('close', () => this.instances[lang].instance = null );
    this.instances[lang].instance = instance;
  }

  /**
   * indefinitely write to instance.stdin()
   * I want to avoid this behavior by just writing one time to stdin
   * But if I write only one time, stdout.on('data') is never called
   * Everything works if I use stdin.end() but I don't want to use it
   */
  write(lang, text) {
    const id = setInterval(() => {
      console.log('setInterval');
      this.instances[lang].instance.stdin.write(text + '\n');
    }, 1000);
  }

};

// simple usage example
const driver = new Driver;
const txt = "This is a random input.";

if (driver.isRunning('enGB') === true)
  driver.write('enGB', txt);
else {
  /** 
   * the arrow function is called once every N stdin.write()
   * While I want it to be called after each write
   */
  driver.run('enGB', data => console.log('Data received!', data));
  driver.write('enGB', txt);
}
我试图:

  • 在stdin.write()周围使用cork()和uncork()
  • 将child_process.stdout()管道连接到自定义可读的套接字和套接字
  • 在stdin、stdout和前面提到的可读代码中将highWaterMark值重写为1和0
  • 还有很多我忘记的事情
此外,我不能使用stdin.end(),因为我不想在每次新文本到达时杀死我的二进制文件实例。有人有想法吗?

看看这个: