Node.js 节点子进程exec async只调用一次

Node.js 节点子进程exec async只调用一次,node.js,asynchronous,child-process,Node.js,Asynchronous,Child Process,我有一个带有一些ID的数组,比如a=[abc,cde,efg]。我将这个数组和第二个数组传递给节点中的子进程,该数组包含符合ID的名称。 我想等到子进程完成后再处理下一个数组成员 实现它的代码是(和建议的编辑): 我使用npm async来控制并发流,因为我想限制在shell脚本中执行的curl请求。shell sript可以正常工作。现在,由于async.eachOfLimit限制,脚本只被调用了两次。然后不再处理其他阵列ID 编辑 这是我最后尝试过的代码,但是所有可能的URL都会被调用,而不

我有一个带有一些ID的数组,比如
a=[abc,cde,efg]
。我将这个数组和第二个数组传递给节点中的子进程,该数组包含符合ID的名称。 我想等到子进程完成后再处理下一个数组成员

实现它的代码是(和建议的编辑)

我使用npm async来控制并发流,因为我想限制在shell脚本中执行的curl请求。shell sript可以正常工作。现在,由于
async.eachOfLimit
限制,脚本只被调用了两次。然后不再处理其他阵列ID

编辑

这是我最后尝试过的代码,但是所有可能的URL都会被调用,而不是只调用2个。我在这里找到的。我还尝试了async.timesLimit

function downloadSentinel(promObj,req,res) {
     async.eachOfLimit(promObj.requestURLS, 2,function (value,i,callback) {
         console.log('I am here ' + i + 'times');
         url = promObj.requestURLS;
         name = promObj.Name;
         console.log(promObj.requestURLS);
         var sys = require('util'),
             exec = require('child_process').exec,
             child;

         var directory = __dirname.substring(0, __dirname.indexOf("\\app_api"));
         console.log(directory);

         console.log("executing:", directory + '\\downloadProducts.sh ' + promObj.requestURLS[i] + ' ' + promObj.Name[i]);
         child = exec(directory + '\\downloadProducts.sh' + ' ' + promObj.requestURLS[i] + ' ' + promObj.Name[i], function (error, stdout, stderr) {

             if (error != null){
                 console.log(error);
             }



            // this task is resolved
            return  callback(error, stdout);
         });

     }, function (err) {
        if (err) console.log('error')
        else {
            console.log('Done');
        }
    });

     }

我错过了什么?提前感谢。

exec
是异步的,它不会等待启动的进程完成


将调用移动到
callback()
子类中。on('exit')处理程序或使用

只需将回调放在
on('exit')
中即可。谢谢。现在我有一个问题,只有两个数组项被下载,然后什么也没有发生。回调是否结束了整个`async.eachOfLimit()`函数?否,您将eachOfLimit的limit参数设置为2(第二个参数),这限制了异步操作的数量。我对该函数的理解如下:假设数组中有5项。函数
makeRequest
被调用2次,以便从数组中下载前两项。回调后,下载接下来的两个项目,依此类推。我想得对吗?是的,你想得对,我误解了极限的含义。重读文档很明显,当所有项都被迭代或出现错误时(这应该会停止迭代),会调用eachOffLimit回调,因此可能就是您的情况。我发现,根据哪个限制,我设置了调用子进程的次数。回调后,函数结束,不再调用。有什么想法吗?
function downloadSentinel(promObj,req,res) {
     async.eachOfLimit(promObj.requestURLS, 2,function (value,i,callback) {
         console.log('I am here ' + i + 'times');
         url = promObj.requestURLS;
         name = promObj.Name;
         console.log(promObj.requestURLS);
         var sys = require('util'),
             exec = require('child_process').exec,
             child;

         var directory = __dirname.substring(0, __dirname.indexOf("\\app_api"));
         console.log(directory);

         console.log("executing:", directory + '\\downloadProducts.sh ' + promObj.requestURLS[i] + ' ' + promObj.Name[i]);
         child = exec(directory + '\\downloadProducts.sh' + ' ' + promObj.requestURLS[i] + ' ' + promObj.Name[i], function (error, stdout, stderr) {

             if (error != null){
                 console.log(error);
             }



            // this task is resolved
            return  callback(error, stdout);
         });

     }, function (err) {
        if (err) console.log('error')
        else {
            console.log('Done');
        }
    });

     }