简体   繁体   中英

Not receiving stdout from nodejs spawned process

I'm trying to have nodejs interact with adventure , an old text based game. The idea is to open adventure as a child process and then play the game by writing to its stdin and placing an event listener on stdout .

When the game starts, it prints an initial:

Welcome to Adventure!! Would you like instructions?

So to illustrate my problem, I have a nodejs+express instance with:

var childProcess = require('child_process');
var spawn = childProcess.spawn;
var child = spawn('adventure');

console.log("spawned: " + child.pid);

child.stdout.on('data', function(data) {
  console.log("Child data: " + data);
});
child.on('error', function () {
  console.log("Failed to start child.");
});
child.on('close', function (code) {
  console.log('Child process exited with code ' + code);
});
child.stdout.on('end', function () {
  console.log('Finished collecting data chunks.');
});

But when I start the server, the text from the game doesn't reach the event listener:

spawned: 24250

That's all the output I get. The child.stdout.on even listener is never called. Why isn't that initial line from the game being picked up?

If I append the following line to the above block of javascript, then the program output appears at once. So adventure runs, and I can now force it to trigger the child.stdout.on event listener... but this also ends the child process, which defeats the purpose of reading and writing to it.

...
child.stdout.on('end', function () {
  console.log('Finished collecting data chunks.');
});
child.stdin.end();

Now the output is:

spawned: 28778  
Child data:
Welcome to Adventure!!  Would you like instructions?
user closed input stream, quitting...

Finished collecting data chunks.
Child process exited with code 0

I'm sure its a trivial oversight on my part, I appreciate any help figuring this out though.

After going through the Nodejs documentation a few more times, I convinced myself I was either missing something pretty big, or the spawn command wasn't working correctly. So I created a github issue .

And the answer was immediately posted: the child process can't buffer output if you want fast access.

So to achieve what I was originally looking for:

var childProcess = require('child_process');
var spawn = childProcess.spawn;
var child = spawn('unbuffer', 'adventure');

console.log("spawned: " + child.pid);

child.stdout.on('data', function(data) {
  console.log("Child data: " + data);
});
child.on('error', function () {
  console.log("Failed to start child.");
});
child.on('close', function (code) {
  console.log('Child process exited with code ' + code);
});
child.stdout.on('end', function () {
  console.log('Finished collecting data chunks.');
});

With the functional difference being the use of unbuffer , a command that disables output buffering.

Why isn't that initial line from the game being picked up?

I had the same problem on a project that called a compiled C++ program from node.js. I realized the problem was in the compiled C++ program: I wasn't flushing the stdout stream. Adding fflush(stdout); after printing a line solved the issue. Hopefully you still have access to the source of the game!

The data passed is a buffer type, not a string. Therefore, you need a decoder to read that buffer and then do the logging.

Here's how to do that.

var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');

child.stdout.on('data', function (data) {
    var message = decoder.write(data);
    console.log(message.trim());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM