Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
batch run
const chunkSize = 3;
const numberOfTasks = 10;
let tasksSleepInput = Array(numberOfTasks).fill(30).map(v => ({delay: v, index: '' }));
// add indices
Object.keys(tasksSleepInput).forEach(k => tasksSleepInput[Number.parseInt(k)].index = k);
[LOG]: "* start running batch 0"
[LOG]: " --> Task 0 started"
[LOG]: " --> Task 1 started"
[LOG]: " --> Task 2 started"
[LOG]: " <-- Task 0 finished"
[LOG]: " <-- Task 1 finished"
[LOG]: " <-- Task 2 finished"
[LOG]: "* finished running batch 0"
[LOG]: "* start running batch 3"
[LOG]: " --> Task 3 started"
[LOG]: " --> Task 4 started"
[LOG]: " --> Task 5 started"
[LOG]: " <-- Task 3 finished"
[LOG]: " <-- Task 4 finished"
[LOG]: " <-- Task 5 finished"
[LOG]: "* finished running batch 3"
[LOG]: "* start running batch 6"
[LOG]: " --> Task 6 started"
[LOG]: " --> Task 7 started"
[LOG]: " --> Task 8 started"
[LOG]: " <-- Task 6 finished"
[LOG]: " <-- Task 7 finished"
[LOG]: " <-- Task 8 finished"
[LOG]: "* finished running batch 6"
[LOG]: "* start running batch 9"
[LOG]: " --> Task 9 started"
[LOG]: " <-- Task 9 finished"
[LOG]: "* finished running batch 9"
async function solution1() {
for(let i = 0; i < tasksSleepInput.length; i+= chunkSize) {
let batch = tasksSleepInput.slice(i, i + chunkSize);
console.log(`* start running batch ${i}`);
await Promise.all(batch.map(taskExecutor));
console.log(`* finished running batch ${i}`);
}
}
let idx =1, tasksQueue = Array(numberOfTasks).fill(1)
.map(v => Math.round(Math.random() * 30) + v)
.map(v => (() => taskExecutor({delay: v, index: `${idx++}` })));
await Promise.all([worker(1), worker(2), worker(3), worker(4)]);
let worker = async (index: number) => {
console.log(`worker ${index} is started`);
while(tasksQueue.length > 0) {
console.log(`worker ${index} handles task`);
let task = tasksQueue[0]; // should be atomic
tasksQueue = tasksQueue.slice(1);
await task();
console.log(`worker ${index} finished handling task`);
}
console.log(`worker ${index} is retired`);
};
[LOG]: "Total tasks: 10"
[LOG]: "worker 1 is started"
[LOG]: "worker 1 handles task"
[LOG]: " --> Task 1 started"
[LOG]: "worker 2 is started"
[LOG]: "worker 2 handles task"
[LOG]: " --> Task 2 started"
[LOG]: "worker 3 is started"
[LOG]: "worker 3 handles task"
[LOG]: " --> Task 3 started"
[LOG]: "worker 4 is started"
[LOG]: "worker 4 handles task"
[LOG]: " --> Task 4 started"
[LOG]: " <-- Task 4 finished"
[LOG]: "worker 4 finished handling task"
[LOG]: "worker 4 handles task"
[LOG]: " --> Task 5 started"
[LOG]: " <-- Task 3 finished"
[LOG]: "worker 3 finished handling task"
[LOG]: "worker 3 handles task"
[LOG]: " --> Task 6 started"
[LOG]: " <-- Task 1 finished"
[LOG]: "worker 1 finished handling task"
[LOG]: "worker 1 handles task"
[LOG]: " --> Task 7 started"
[LOG]: " <-- Task 2 finished"
[LOG]: "worker 2 finished handling task"
[LOG]: "worker 2 handles task"
[LOG]: " --> Task 8 started"
[LOG]: " <-- Task 6 finished"
[LOG]: "worker 3 finished handling task"
[LOG]: "worker 3 handles task"
[LOG]: " --> Task 9 started"
[LOG]: " <-- Task 5 finished"
[LOG]: "worker 4 finished handling task"
[LOG]: "worker 4 handles task"
[LOG]: " --> Task 10 started"
[LOG]: " <-- Task 8 finished"
[LOG]: "worker 2 finished handling task"
[LOG]: "worker 2 is retired"
[LOG]: " <-- Task 7 finished"
[LOG]: "worker 1 finished handling task"
[LOG]: "worker 1 is retired"
[LOG]: " <-- Task 10 finished"
[LOG]: "worker 4 finished handling task"
[LOG]: "worker 4 is retired"
[LOG]: " <-- Task 9 finished"
[LOG]: "worker 3 finished handling task"
[LOG]: "worker 3 is retired"
let taskExecutor = async (task: {delay: number, index: string}) => {
console.log(` --> Task ${task.index} started`);
await sleep(task.delay);
console.log(` <-- Task ${task.index} finished`);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment