承诺实现阻塞队列

时间:2020-10-21 04:27:14

标签: node.js express promise

我想限制对exec命令的调用次数。 我检查了Promise.map API,如下所示:

Promise.map(
    Iterable<any>|Promise<Iterable<any>> input,
    function(any item, int index, int length) mapper,
    [Object {concurrency: int=Infinity} options]
) -> Promise

我想用它来限制所执行的exec命令和所有其他调用的数量,以等待并发级别低于该限制。 因此,基本上下面的程序将继续调用exec,从而派生本机进程。我想限制要分叉的本机进程的数量。因此,我需要保持对调用exec的任何请求超出限制N,使其处于阻塞状态,直到Promise Map可以承担新任务。这类似于在Java中阻止队列实现。

app.post('/upload',function(req,res){  
    upload(req,res,function(err) {  
        if(err) {  
            return res.end("Error uploading file.");  
        }  
        
        console.log("Coverting to PDF");
        

        var inputfile = "./uploads/" + req.file.filename;
        var outfile = "./exports/" + req.file.filename + ".pdf";
        var command = '"sample.exe"' + " \"" + inputfile +  "\" "  +  "\"" + outfile + "\"" ;
        console.log(command);
    
        /*I want to limit the call to below code to some number N and all N+1 calls to wait until the number of native execution goes below N*/                 
        exec(command,(error, stdout, stderr) => {
                        console.log("trying to download file " + outfile);
                        res.download(outfile);

            
                            });
        console.log("Coversion Done");
        

    });  
});  

0 个答案:

没有答案