Now has a job queue.

This commit is contained in:
Fabian Seluska 2021-10-20 10:03:48 +02:00
parent 35a14b4362
commit e760e6bd38
2 changed files with 60 additions and 52 deletions

View File

@ -6,71 +6,79 @@ const log = initLog("Worker Setup")
let workerFarm = [] let workerFarm = []
let workerReady = [] let workerReady = []
let scheduledJobs = []
function waitForThread() { function scheduler() {
return new Promise( if (scheduledJobs.length === 0) return
(resolve, reject) => {
let interval = setInterval( const readyIndex = workerReady.indexOf(true)
() => { if (readyIndex === -1) return
const readyIndex = workerReady.indexOf(true)
if (readyIndex !== -1) { workerReady[readyIndex] = false
clearInterval(interval) const worker = workerFarm[readyIndex]
resolve(readyIndex)
} const job = scheduledJobs.shift()
},
50 worker.postMessage({
type: 'runScript',
script: job.script,
data: job.data,
id: job.jobID
})
const messageHandler = message => {
if (message.id === job.jobID) {
workerReady[readyIndex] = true
worker.removeListener(
'message',
messageHandler
) )
return job.callback(message.result)
} }
}
worker.on(
'message',
messageHandler
) )
} }
export function initWorkers(threads) { export function initWorkers(threads) {
for (let i = 0; i < threads; ++i) { for (let i = 0; i < threads; ++i) {
workerFarm.push(new Worker('./libs/utils/multithread/genericWorker.js', { env: SHARE_ENV })) workerFarm.push(new Worker('./libs/utils/multithread/genericWorker.js', { env: SHARE_ENV }))
}
for (const i in workerFarm) {
const worker = workerFarm[i] const worker = workerFarm[i]
worker.postMessage({ worker.postMessage({
type: 'initWorker', type: 'initWorker',
name: `Worker ${parseInt(i) + 1}` name: `Worker ${parseInt(i) + 1}`
}) })
workerReady[i] = true workerReady[i] = true
} }
log(`Initialised ${threads} workers!`) log(`Initialised ${threads} workers!`)
setInterval(
scheduler,
50
)
log(`Initialised the scheduler!`)
} }
export function spread(script, data) { export function spread(script, data) {
return new Promise( return new Promise(
async (resolve, reject) => { async (resolve, reject) => {
const readyIndex = await waitForThread()
workerReady[readyIndex] = false
const worker = workerFarm[readyIndex]
const jobID = randomUUID() const jobID = randomUUID()
worker.postMessage({
type: 'runScript', scheduledJobs.push({
script, script,
data, data,
id: jobID jobID,
}) callback: result => {
resolve(result)
const messageHandler = message => {
if (message.id === jobID) {
workerReady[readyIndex] = true
worker.removeListener(
'message',
messageHandler
)
resolve(message.result)
} }
} })
worker.on(
'message',
messageHandler
)
} }
) )
} }

View File

@ -5,23 +5,23 @@ import initLog from '../libs/utils/log.js'
const log = initLog("Fib. Test") const log = initLog("Fib. Test")
initWorkers(10) initWorkers(3)
// const jobs = 100 const jobs = 10
// let completed = 0 let completed = 0
// for (let i = 0; i < jobs; ++i) { for (let i = 0; i < jobs; ++i) {
// spread( spread(
// './fibonacci.js', './fibonacci.js',
// {} {}
// ).then( ).then(
// result => { result => {
// ++completed ++completed
// log(`Completed job ${completed}/${jobs} -> ${result}`) log(`Completed job ${completed}/${jobs} -> ${result}`)
// if (completed === jobs) process.exit(0) if (completed === jobs) process.exit(0)
// } }
// ) )
// } }