我有一个任务列表,只有在解决每个任务的所有依赖关系后才需要执行所有这些任务。我正在努力找出一种方法来在最佳时间完成所有任务的运行。
// Each node is a async job, illustrated by setTimeout.
// A and C can run at the same time.
// D, needs to wait for A and C to be done.
// E needs to wait for A and D to be done.
function runTasks(tasks) {
// run tasks
}
// Sample of tasks
var tasks = {
'a': {
job: function (finish) {
setTimeout(function () {
console.log('a done');
finish();
}, 500);
},
},
'c': {
job: function (finish) {
setTimeout(function () {
console.log('c done');
finish();
}, 200);
},
dependencies: [],
},
'd': {
job: function (finish) {
setTimeout(function () {
console.log('d done');
finish();
}, 100);
},
dependencies: ['a','c'],
},
'e': {
job: function (finish) {
setTimeout(function () {
console.log('e done');
finish();
}, 200);
},
dependencies: ['a', 'd'],
},
};
最佳答案
您可以启动可以启动的并行 promise 。
- 首先启动所有没有依赖关系的独立任务
- 完成独立任务后存储在 map /集合中
- 然后使用整套重复步骤过滤一组新任务。
const wait = (ms, task) =>
new Promise((resolve) =>
setTimeout(() => {
console.log(`${task} done`);
resolve();
}, ms)
);
const tasks = {
a: {
job: () => wait(500, "A"),
dependencies: [],
},
c: {
job: () => wait(500, "C"),
dependencies: [],
},
d: {
job: () => wait(500, "D"),
dependencies: ["a", "c"],
},
e: {
job: () => wait(500, "E"),
dependencies: ["a", "d"],
},
};
const run = async () => {
const completed = new Set();
const keys = Object.keys(tasks);
const runTasks = async (pendingTasks) => {
const promises = pendingTasks.map((key) => tasks[key].job());
await Promise.all(promises);
pendingTasks.forEach((key) => completed.add(key));
};
const runner = async () => {
const pendingTasks = keys.filter((key) => {
if (!completed.has(key)) {
const { dependencies } = tasks[key];
return dependencies.every((dependency) => completed.has(dependency));
}
return false;
});
if (pendingTasks.length !== 0) {
await runTasks(pendingTasks);
return runner();
}
};
await runner();
};
run();
关于javascript - Javascript 中的任务运行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72137667/