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使用外部二进制文件。根据语言,每个二进制搜索字符串中的精确单词,并根据输入文本生成输出。它们没有内部缓冲区。用法示例:
生成的文本类似于句子中的单词Xecho“我是一个随机输入”|/我的英语二进制文件
产生无限输出cat/dev/uradom |./my english binary
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
- 还有很多我忘记的事情