使用节点child_process的stdout缓冲区问题 [英] Stdout buffer issue using node child_process

查看:1955
本文介绍了使用节点child_process的stdout缓冲区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用节点child_process执行curl,以从本地网络中的共享文件夹获取JSON文件(大约220Ko)。但它实际上返回了一个我无法解决的缓冲问题。
这是我的代码:

I'm trying to execute curl using node child_process to get a JSON file (about 220Ko) from a shared folder in a local network. But it actually returns a buffer problem that I can't get throught. Here is my code:

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

var execute = function(command, callback){
    exec(command, function(error, stdout, stderr){ callback(error, stdout); });
};

execute("curl http://" + ip + "/file.json", function(err, json, outerr) {
    if(err) throw err;
    console.log(json);
})

这是我得到的错误:

if(err) throw err;
          ^
Error: stdout maxBuffer exceeded.
    at Socket.<anonymous> (child_process.js:678:13)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:746:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
    at Pipe.onread (net.js:526:21)


推荐答案

你需要使用 child_process.exec 时使用和设置 maxBuffer 选项。来自文档

You need to use and set the maxBuffer option when using child_process.exec. From the documentation:


maxBuffer 指定stdout或stderr上允许的最大数据量 - 如果超过此值,子进程将被终止。

maxBuffer specifies the largest amount of data allowed on stdout or stderr - if this value is exceeded then the child process is killed.

该文档还指出 maxBuffer 的默认值为200KB。

The documentation also states that the default value of maxBuffer is 200KB.

例如,在以下代码中,最大缓冲区大小增加到500KB:

As an example, the maximum buffer size is increased to 500KB in the following code:

var execute = function(command, callback){
    exec(command, {maxBuffer: 1024 * 500}, function(error, stdout, stderr){ callback(error, stdout); });
};

此外,您可能想了解 http.get ,了解它是否能够实现您的目标。

Additionally, you may want to read about http.get to see if it is capable of achieving what you are trying to do.

这篇关于使用节点child_process的stdout缓冲区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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