我正在制作一个基于 Web 的工具,它可以连接到一个名为 RetroPie-Setup 的现有基于 shell 的框架。 .
他们有一个名为/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}
Node 代码:
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(),它又返回一个 Promise,该 Promise 立即开始执行其中的内容。要解决这个问题,您可以使用 Function.prototype.bind()
将参数绑定(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 解析或拒绝,这意味着您有一个按顺序执行的队列。
关于node.js - NodeJS 队列多个 execFile 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46616218/