问题
Node的child-process有限制吗?我怎样才能用一个巨大的文件来获取一个巨大的命令输出,比如git显示的命令输出呢?
一般问题
我试图解析git show <commit sha>的结果,有一个大文件(308344行)
在运行git show > showed_by_git.txt时,我有正确的输出和所有文件,结果是118 667行
当运行节点的child-process时,我只检索32 602行.
我的测试及其结果
我简化了我的代码,并在停止流之前使用child-process来计算字符数和它解析的行数。
结果表明,它停止在32 000+行,而不是预期的118 667行。
如果您有一个包含一些HUUUGE文件的存储库,那么您可以在家里复制这个库,该文件是最近被介绍的文件。
const childProcess = require('child_process')
function fetchCommand (command) {
return new Promise((resolve, reject) => {
const sub = childProcess.exec(command)
let chars = 0
let lines = 0
sub.stdout.on('data', function (chunk) {
chars += chunk.length
lines += chunk.split('\n').length
console.log('chars:' + chars + ' lines:' + lines)
// logs the char and line count on each chunk of data,
// then 'forgets' the data : no memory overloading
})
sub.stdout.on('close', function () {
console.log('CLOSED')
})
sub.stderr.on('error', function (err) {
console.log('ERROR: ' + err.message)
})
})
}
fetchCommand('git show').catch(err => console.log(err))输出
这是输出:
C:\Users\guill\.code\git2stats>node examples/fetchTest.js
chars:4096 lines:126
chars:73728 lines:2117
chars:131072 lines:3772
chars:176128 lines:5176
chars:229376 lines:6560
chars:262144 lines:7663
chars:323584 lines:9171
chars:393216 lines:11304
chars:462848 lines:13483
chars:475136 lines:13849
chars:507904 lines:14916
chars:536576 lines:15839
chars:573440 lines:17028
chars:618496 lines:18484
chars:688128 lines:20539
chars:737280 lines:22000
chars:765952 lines:22930
chars:794624 lines:23860
chars:823296 lines:24794
chars:892928 lines:26976
chars:962560 lines:29104
chars:991232 lines:30003
chars:1032192 lines:31292
chars:1073152 lines:32602
CLOSED您可以看到它停在32 602行,而这个特定的git show有118667行要显示。
最后一块
我检查了最后一块数据,看看它是否对大文件做了一些特别的事情,但是我可以确认它就停在文件的中间。
上下文
我正在编写一个git统计工具,这个程序非常好,因为我可以解析git log --stat,然后对每个提交进行git show <commit sha>,并返回一个令人满意的json。
发布于 2020-08-07 14:40:55
这是一种我以前不知道的诺德脚枪。
是的,是有限度的。它配置了maxBuffer选项(见文档),如果愿意,可以将其设置为Infinity。可以设置为Infinity的想法没有文档化。
const sub = childProcess.exec(command, { maxBuffer: Infinity });我对这个限制的存在感到震惊,现在我将被迫在大量代码中进行代码检查,以查找使用child_process模块的每个地方,并查看是否需要添加maxBuffer选项。
以它为例,说明如何糟糕地设计接口。
小音符
您可能希望处理相同的sub.on('exit', code => {})或sub.on('close'),因此您可以检查Git的退出状态,如果状态不是0,则会引发错误。就像这样:
sub.on('exit', code => {
if (code == 0) {
resolve(...);
} else {
reject(...);
}
});https://stackoverflow.com/questions/63303225
复制相似问题