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(
script, (resolve, reject) => {
{ const worker = workerFarm[spreadIndex]
workerData: data, worker.postMessage({
eval: shouldEval ?? false type: 'runScript',
} script,
) data
})
worker.on('message', resolve) worker.once('message', result => resolve(result))
worker.on('error', reject)
worker.on('exit', (code) => { ++spreadIndex
if (code !== 0) if(spreadIndex >= workerFarm.length)
reject(new Error(`Process stopped with code: ${code}`)) spreadIndex = 0
}) }
}) )
} }

View File

@ -1,23 +1,29 @@
import { workerData, parentPort } from 'worker_threads' import initLog from '../log.js'
let nums = [1, 1] const log = initLog('Fibonacci')
for (let i = 0; i < 42; i++) { export function main(data) {
nums.push(nums[nums.length - 2] + nums[nums.length - 1]) return new Promise(
(resolve, reject) => {
let nums = [1, 1]
for (let i = 0; i < 44; i++) {
nums.push(nums[nums.length - 2] + nums[nums.length - 1])
}
let primes = []
for (const num of nums) {
let isPrime = true
for (let i = 2; i < num / 2; ++i) {
if (num % i === 0)
isPrime = false
}
if (isPrime)
primes.push(num)
}
resolve(primes)
}
)
} }
let primes = []
for (const num of nums) {
let isPrime = true
for (let i = 2; i < num / 2; ++i) {
if (num % i === 0)
isPrime = false
}
if (isPrime)
primes.push(num)
}
console.log("DONE!")
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)
}
}
)
}