[英]Sending input data to child process in node.js
I am writing code to create an online c++ compiler in node.js environment.Using spawn
function i created a child process which will compile the code and execute it and send the output back to the user. 我正在编写代码以在node.js环境中创建在线c ++编译器。使用
spawn
函数,我创建了一个子进程,该子进程将编译代码并执行并将输出发送回用户。 But i need to send input to the running c++ program . 但是我需要将输入发送到正在运行的c ++程序。 I used
child.stdin.write('data');
我用
child.stdin.write('data');
for sending data to child but i think cin
in the program is not receiving the input . 用于向孩子发送数据,但我认为程序中的
cin
没有收到输入。
Please help me to send the input to the running c++ code. 请帮助我将输入发送到正在运行的c ++代码。
Thanks. 谢谢。
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 communicate via process.send 如果您想传递消息,则可能应该使用集群,也可以使用fork。如果这样做,则节点将设置IPC,以便您能够通过process.send进行通信。
Alternatively, you could use a pub/sub system for communication channels (Redis works well for this), this is also your best bet if you need communications across servers. 另外,您可以将发布/订阅系统用于通讯渠道(Redis对此非常有效),如果您需要跨服务器通讯,这也是最好的选择。
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);
}
}
1.Create a file with input data. 1.使用输入数据创建文件。
2.Create a ReadStream
opening input file. 2.创建一个
ReadStream
打开输入文件。
3.Pipe the ReadStream
with childprocess.stdin
. 3.Pipe的
ReadStream
与childprocess.stdin
。
file=fs.createReadStream('input.txt',{encoding:'utf8'});
file.pipe(childprocess.stdin);
This worked for me . 这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.