Skip to content

Instantly share code, notes, and snippets.

@OMGZui
Created November 18, 2022 09:40
Show Gist options
  • Save OMGZui/2f3ebf17e8452228b73b84788959820c to your computer and use it in GitHub Desktop.
Save OMGZui/2f3ebf17e8452228b73b84788959820c to your computer and use it in GitHub Desktop.
cluster.setupPrimary({
exec: resolve(__dirname, 'features/process.js')
})
const works: Array<{
work: Worker;
tasks: Array<imageType>
}> =[]
if (list.length <= cpuNums) {
works.push({
work: cluster.fork(),
tasks: list
})
} else {
for (let i = 0; i < cpuNums; ++i) {
const work = cluster.fork()
works.push({
work,
tasks: []
})
}
}
let workNum = 0
list.forEach(task = >{
if (works.length === 1) {
return
} else if (workNum >= works.length) {
works[0].tasks.push(task)
workNum = 1
} else {
works[workNum].tasks.push(task)
workNum += 1
}
})
let pageNum = works.length
works.forEach(({
work,
tasks
}) = >{
work.send(tasks)
work.on('message', (details: Idetail[]) = >{
pageNum--
if (pageNum === 0) {
cluster.disconnect()
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment