Created
December 12, 2021 08:12
-
-
Save exhesham/32ac56f2b99ccaba92aff043e0642748 to your computer and use it in GitHub Desktop.
batch run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let idx =1, tasksQueue = Array(numberOfTasks).fill(1) | |
.map(v => Math.round(Math.random() * 30) + v) | |
.map(v => (() => taskExecutor({delay: v, index: `${idx++}` }))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await Promise.all([worker(1), worker(2), worker(3), worker(4)]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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