我使用的是来自exec的child_process。
该函数运行良好,但在4-5分钟后,它只是停止,没有任何错误报告,但脚本应该运行至少24小时.
以下是代码:
import { exec } from 'child_process';
function searchDirectory(dirPath) {
let lineBuffer = '';
const cmd = `find ${dirPath} -type f -name "*.txt" | pv -l -L 10 -q`;
const findData = exec(cmd);
findData.on('error', err => log.error(err));
findData.stdout.on('data', data => {
lineBuffer += data;
let lines = lineBuffer.split('\n');
for (var i = 0; i < lines.length - 1; i++) {
let filepath = lines[i];
processfile(filepath);
}
lineBuffer = lines[lines.length - 1];
});
findData.stdout.on('end', () => console.log('finished finding...'));
}pv命令减慢了输出速度,因为我发现的路径是网络上的,并且非常慢(60 is /s)。
当我在终端中直接运行命令时,它可以正常工作(我没有等待24小时,但我让它运行了半小时,它仍然在运行)。
processfile函数实际上使用axios进行异步调用,将一些数据发送到服务器:
let data = readFileSync(file);
...
axios.post(API_URL, { obj: data }, { proxy: false })
.then(res => {
log.info('Successfully saved object : ' + res.data._id);
})
.catch(err => {
log.error(err.response ? err.response.data : err);
});是什么让剧本停止了?有什么想法吗?
谢谢
发布于 2018-11-28 15:09:50
我发现了这个问题,不建议对巨大的输出使用exec,因为它使用的是有限大小的缓冲区。使用spawn代替:
child_process.spawn和child_process.exec之间最显著的区别在于它们返回的内容--生成返回流,而exec返回缓冲区。 child_process.spawn返回一个具有stdout和stderr流的对象。您可以点击stdout流来读取子进程发送回Node的数据。stdout是一个流,具有“数据”、“结束”和流所具有的其他事件。当您希望子进程将大量的数据返回给节点图像处理、读取二进制数据等时,产卵是最常用的。 child_process.exec返回子进程的整个缓冲区输出。默认情况下,缓冲区大小设置为200 K。如果子进程返回更多内容,您的程序将崩溃,错误消息为" error : maxBuffer If“。可以通过在exec选项中设置更大的缓冲区大小来解决此问题。但是,您不应该这样做,因为exec并不是用于将巨大缓冲区返回给Node的进程。你应该用产卵来做这个。所以你用主管做什么?使用它来运行返回结果状态的程序,而不是数据。
来自:process.html
https://stackoverflow.com/questions/53520947
复制相似问题