NodeJS 将多个可执行文件调用排队



我正在制作一个基于Web的工具,该工具可以连接到现有的基于shell的框架RetroPie-Setup。

他们有一个名为/RetroPie-Setup/retropie_packages.sh 的 shell 脚本,您可以使用它来安装、获取依赖项,甚至编译不同的程序。

一个问题是 packages.sh 不应该在给定时刻运行多次,所以我需要设置一个一次运行一个的队列。

我想我可以使用 promise-queue 来防止多次执行,但是每当我运行 execFile 时,它最终都会立即运行命令,而不是在它到达队列中的某个位置时运行命令。

这是我的示例代码:

downloadTest.sh (下载具有唯一名称的 10Mb 文件):

filename=test$(date +%H%M%S).db
wget -O ${filename} speedtest.ftp.otenet.gr/files/test10Mb.db
rm ${filename}

节点代码

const Queue = require('promise-queue')
const { spawn,execFile } = require('child_process');
var maxConcurrent = 1;
var maxQueue = Infinity;
var que = new Queue(maxConcurrent, maxQueue);
var testFunct = function(file)
{
    var promise = new Promise((reject,resolve) => {
        execFile(file,function(error, stdout, stderr) {
            console.log('Finished executing');
            if(error)
            {
                reject();
            } else
            {
                resolve(stdout);
            }
        });
    })
    return promise;
}
var test1 = testFunct('/home/pi/downloadTest.sh')
var test2 = testFunct('/home/pi/downloadTest.sh')
var test3 = testFunct('/home/pi/downloadTest.sh')
que.add(test1);
que.add(test2);
que.add(test3);

你的代码非常接近工作。主要问题是你正在执行testFunct(),这反过来又返回一个承诺,立即开始执行其中的内容。要解决此问题,您可以使用Function.prototype.bind()将参数绑定到函数,而无需执行它。喜欢这个:

que.add(testFunct.bind(null, '/home/pi/downloadTest.sh'));
que.add(testFunct.bind(null, '/home/pi/downloadTest.sh'));
que.add(testFunct.bind(null, '/home/pi/downloadTest.sh'));

或者,您可以使用async/await,这使得队列变得微不足道,这反过来又允许您放弃对promise-queue的依赖。

const execFile = require("util").promisify(require("child_process").execFile)
(async function () {
  let scripts = [
    "/home/pi/downloadTest.sh",
    "/home/pi/downloadTest.sh",
    "/home/pi/downloadTest.sh"
  ]
  for (let script of scripts) {
    try {
      let { stdout, stderr } = await execFile(script)
      console.log("successfully executed script:", script)
    } catch (e) {
      // An error occured attempting to execute the script
    }
  }
})()

上面代码中有趣的部分是 await execFile(script) .await表达式时,整个函数的执行将暂停,直到execFile函数返回的 Promise 解析或拒绝,这意味着您有一个按顺序执行的队列。

最新更新