Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually flush a pipe to a child process in node.js?

I am trying to run the a script on node.js which sequentially sends numbers to an executable a.out, which squares it and writes the result on its stdout. When that happens, the next number will be sent. Here's the code:

var spawn = require('child_process').spawn;
var p = spawn("./a.out",[],{"stdio":"pipe"});
p.stdout.setEncoding("utf8");
p.stdout.on("data",function(data){
    var x = parseInt(data.trim()), y = Math.sqrt(x);
    console.log("received",x);
    if(++y===10) p.kill();
    else {
        console.log("sending",y);
        p.stdin.write(y+"\n");
    }
});
var start = 2;
console.log("sending",start);
p.stdin.write(start+"\n");
setTimeout(function(){ p.stdin.end(); },1000);

where a.out is the compiled version to the following C program:

#include<stdio.h>
int main(){
    int x;
    while(scanf("%d",&x)!=EOF)
        printf("%d\n",x*x);
    return 0;
}

However, I am getting the following output:

sending 2
received 4
sending 3

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: write after end

Changing the milliseconds value in setTimeout only delays the error. Apparently, the data that I'm trying to send to a.out is being buffered and sent only when I terminate the pipe. How do I flush it manually?

like image 378
Kaustubh Karkare Avatar asked Nov 02 '22 22:11

Kaustubh Karkare


1 Answers

I have been working on this all day.

The problem is that the STDOUT of your spawned process needs to flush it's output buffer, otherwise it will sit there until it fills up, in which case your code won't execute again.

p.stdin.end() only serves to end the process, which by its very nature, allows the OS to clear up all buffers.

You can't do this from the node as this is not the owner of the output buffer.

It is annoying, but as long as you have control of the script, you can modify it there, perhaps allow it to take a command line option to set an autoflush?

Hope this is of some help.

like image 178
user2696489 Avatar answered Nov 06 '22 14:11

user2696489