Working multithreading...

This commit is contained in:
BurnyLlama 2021-10-13 19:45:59 +02:00
parent 747ef0acf9
commit 36ecb5c6a7
4 changed files with 80 additions and 48 deletions

View File

@ -10,27 +10,28 @@ export function initWorkers(threads) {
for (const i in workerFarm) { for (const i in workerFarm) {
const worker = workerFarm[i] const worker = workerFarm[i]
worker.postMessage({ worker.postMessage({
type: 'setName', type: 'initWorker',
name: `Worker ${parseInt(i) + 1}` name: `Worker ${parseInt(i) + 1}`
}) })
} }
} }
export function spread(threads, script, data, shouldEval) { let spreadIndex = 0
return new Promise((resolve, reject) => { export function spread(script, data) {
const worker = new Worker( return new Promise(
(resolve, reject) => {
const worker = workerFarm[spreadIndex]
worker.postMessage({
type: 'runScript',
script, script,
{ data
workerData: data, })
eval: shouldEval ?? false
worker.once('message', result => resolve(result))
++spreadIndex
if(spreadIndex >= workerFarm.length)
spreadIndex = 0
} }
) )
worker.on('message', resolve)
worker.on('error', reject)
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Process stopped with code: ${code}`))
})
})
} }

View File

@ -1,8 +1,13 @@
import { workerData, parentPort } from 'worker_threads' import initLog from '../log.js'
const log = initLog('Fibonacci')
export function main(data) {
return new Promise(
(resolve, reject) => {
let nums = [1, 1] let nums = [1, 1]
for (let i = 0; i < 42; i++) { for (let i = 0; i < 44; i++) {
nums.push(nums[nums.length - 2] + nums[nums.length - 1]) nums.push(nums[nums.length - 2] + nums[nums.length - 1])
} }
@ -18,6 +23,7 @@ for (const num of nums) {
primes.push(num) primes.push(num)
} }
console.log("DONE!") resolve(primes)
}
parentPort.postMessage(primes) )
}

View File

@ -1,30 +1,37 @@
import { workerData, parentPort } from 'worker_threads' import { workerData, parentPort } from 'worker_threads'
import initLog from '../log.js' import initLog from '../log.js'
let script = '' let runScript = ''
let myName = '' let myName = ''
let log = initLog(myName) let log = initLog(myName)
parentPort.on( parentPort.on(
'message', 'message',
message => { async message => {
switch (message.type) { switch (message.type) {
case 'setName': case 'initWorker':
myName = message.name myName = message.name
log = initLog(myName) log = initLog(myName)
log(`Changed name to '${myName}'.`) process.env.DEBUG && log(`Started new thread -> «${myName}»`)
break break
case 'setScript': case 'runScript':
script = message.script runScript = message.script
log = initLog(myName) process.env.DEBUG && log(`Running script -> «${runScript}»`)
log(`Changed name to '${myName}'.`)
break
import(runScript).then(
script => script.main(message.data).then(
result => {
parentPort.postMessage(result)
process.env.DEBUG && log(`Script done -> «${runScript}»`)
}
)
)
break
default: default:
log(`Invalid message -> ${message.type ?? 'No message type provided!'}`)
break break
} }
} }

View File

@ -1,3 +1,21 @@
import { initWorkers, spread } from '../libs/utils/multithread.js' import { initWorkers, spread } from '../libs/utils/multithread.js'
initWorkers(8) initWorkers(10)
let processes = 20
let ran = 0
for (let i = 0; i < processes; ++i) {
spread(
'./fibonacci.js',
{}
).then(
result => {
console.log(i)
++ran
if (ran === processes) {
process.exit(0)
}
}
)
}