Node.js产生EMFILE

nod*_*ser 5 child-process node.js

我试图在我的节点作业中使用ChildProcess.exec在async.forEach循环内运行命令.这是代码

async.forEach( docPaths, function(docPath, callback) { 
 var run = [];
 // some command using docPath variable here..
 run.push(command);
debugger;
 exec(run.join(' '), function(error, stdout, stderr){
    callback();
  });
 }, callback);
Run Code Online (Sandbox Code Playgroud)

这是错误

"stack":"Error: spawn EMFILE\
at errnoException (child_process.js:478:11)\
at ChildProcess.spawn (child_process.js:445:11)\
at child_process.js:343:9\
at Object.execFile (child_process.js:253:15)\
at child_process.js:220:18\
Run Code Online (Sandbox Code Playgroud)

快速谷歌显示我需要设置ulimit值来增加文件描述符的数量可以打开.有点像"ulimit -n 10000"..(来自下面的链接)

https://groups.google.com/forum/#!topic/nodejs/jeec5pAqhps

哪里可以增加这个..?还是有其他解决办法来解决这个问题?

感谢您的帮助..非常感谢!!

Sri*_*sha 8

首先,不建议使用ulimit,因为它可能会产生系统影响.

相反,由于您已经在使用异步,因此它带有一个限制参数,您可以使用它来限制并行执行的次数.

async.eachLimit( docPaths, 100, function(docPath, callback) { 
 var run = [];
 // some command using docPath variable here..
 run.push(command);
debugger;
 exec(run.join(' '), function(error, stdout, stderr){
    callback();
  });
 }, callback);
Run Code Online (Sandbox Code Playgroud)

请进行试验和错误,并用合适的值替换100.