Node.js的stdout child_process exec被缩短了 [英] Stdout of Node.js child_process exec is cut short

查看:106
本文介绍了Node.js的stdout child_process exec被缩短了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node.js中我正在使用child_process模块​​的exec命令来调用Java中的算法,该算法将大量文本返回到标准输出,然后我将其解析并使用。我能够捕获它,但是当它超过一定数量的行时,内容就会被截止。

In Node.js I'm using the exec command of the child_process module to call an algorithm in Java that returns a large amount of text to standard out which I then parse and use. I'm able to capture it mostly, but when it exceeds a certain number of lines, the content is cutoff.

exec("sh target/bin/solver "+fields.dimx+" "+fields.dimy, function(error, stdout, stderr){
    //do stuff with stdout
}

我尝试过使用setTimeouts和回调但没有成功,但我确实觉得这是因为我在我的引用stdout在我可以完全检索之前的代码。我已经测试了stdout实际上是数据丢失首先发生的地方。这不是一个异步问题。我还在我的本地机器和Heroku上测试了这个问题。出现同样的问题,每次都会截断完全相同的行号。

I've tried using setTimeouts and callbacks but haven't succeeded but I do feel this is occurring because I'm referencing stdout in my code before it can be retrieved completely. I have tested that stdout is in-fact where the data loss first occurs. It's not an asynchronous issue further down the line. I've also tested this on my local machine and Heroku, and the exact same issue occurs, truncating at the exact same line number every time.

关于可能有什么帮助的任何想法或建议?

Any ideas or suggestions as to what might help with this?

推荐答案

编辑:
我在我的电脑上试过 dir / s windows )并得到了同样的问题(它看起来l这个代码为我解决了这个问题:

Edited: I have tried with dir /s on my computer (windows) and got the same problem( it look like a bug), this code solve that problem for me:

var exec = require('child_process').exec;

function my_exec(command, callback) {
    var proc = exec(command);

    var list = [];
    proc.stdout.setEncoding('utf8');

    proc.stdout.on('data', function (chunk) {
        list.push(chunk);
    });

    proc.stdout.on('end', function () {
        callback(list.join());
    });
}

my_exec('dir /s', function (stdout) {
    console.log(stdout);
})

这篇关于Node.js的stdout child_process exec被缩短了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆