Started experimenting with multithreading.
This commit is contained in:
parent
4dc248c8b4
commit
747ef0acf9
2
index.js
2
index.js
|
@ -8,7 +8,7 @@ import express from 'express'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import sqlite2mongo from './libs/database/sqlite2mongo.js'
|
import sqlite2mongo from './libs/database/sqlite2mongo.js'
|
||||||
import databaseInit from './libs/database/init.js'
|
import databaseInit from './libs/database/init.js'
|
||||||
import initLog from './libs/utills/log.js'
|
import initLog from './libs/utils/log.js'
|
||||||
import api from './api/api.js'
|
import api from './api/api.js'
|
||||||
|
|
||||||
const log = initLog("[ MAIN ]")
|
const log = initLog("[ MAIN ]")
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import initLog from '../utills/log.js'
|
import initLog from '../utils/log.js'
|
||||||
import mongoose from 'mongoose'
|
import mongoose from 'mongoose'
|
||||||
import sqlite3 from 'sqlite3'
|
import sqlite3 from 'sqlite3'
|
||||||
import { open } from 'sqlite'
|
import { open } from 'sqlite'
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import initLog from '../utills/log.js'
|
import initLog from '../utils/log.js'
|
||||||
import { checkForFinishes, checkForMaps, checkForPlayers } from './sqlite2mongo/checks.js'
|
import { checkForFinishes, checkForMaps, checkForPlayers } from './sqlite2mongo/checks.js'
|
||||||
import postTasks from './sqlite2mongo/postTasks.js'
|
import postTasks from './sqlite2mongo/postTasks.js'
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { sqlite } from "../init.js"
|
||||||
import Level from '../../../schemas/Level.js'
|
import Level from '../../../schemas/Level.js'
|
||||||
import Player from '../../../schemas/Player.js'
|
import Player from '../../../schemas/Player.js'
|
||||||
import Finish from '../../../schemas/Finish.js'
|
import Finish from '../../../schemas/Finish.js'
|
||||||
import initLog from '../../utills/log.js'
|
import initLog from '../../utils/log.js'
|
||||||
|
|
||||||
|
|
||||||
const log = initLog("sqlite2mongo")
|
const log = initLog("sqlite2mongo")
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import Level from '../../../schemas/Level.js'
|
import Level from '../../../schemas/Level.js'
|
||||||
import Player from '../../../schemas/Player.js'
|
import Player from '../../../schemas/Player.js'
|
||||||
import initLog from '../../utills/log.js'
|
import initLog from '../../utils/log.js'
|
||||||
import decodeMsgpack from './decodeMsgpack.js'
|
import decodeMsgpack from './decodeMsgpack.js'
|
||||||
|
|
||||||
const log = initLog("sqlite2mongo")
|
const log = initLog("sqlite2mongo")
|
||||||
|
|
36
libs/utils/multithread.js
Normal file
36
libs/utils/multithread.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import { Worker } from 'worker_threads'
|
||||||
|
|
||||||
|
let workerFarm = []
|
||||||
|
|
||||||
|
export function initWorkers(threads) {
|
||||||
|
for (let i = 0; i < threads; ++i) {
|
||||||
|
workerFarm.push(new Worker('./libs/utils/multithread/genericWorker.js'))
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const i in workerFarm) {
|
||||||
|
const worker = workerFarm[i]
|
||||||
|
worker.postMessage({
|
||||||
|
type: 'setName',
|
||||||
|
name: `Worker ${parseInt(i) + 1}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function spread(threads, script, data, shouldEval) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const worker = new Worker(
|
||||||
|
script,
|
||||||
|
{
|
||||||
|
workerData: data,
|
||||||
|
eval: shouldEval ?? false
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
worker.on('message', resolve)
|
||||||
|
worker.on('error', reject)
|
||||||
|
worker.on('exit', (code) => {
|
||||||
|
if (code !== 0)
|
||||||
|
reject(new Error(`Process stopped with code: ${code}`))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
23
libs/utils/multithread/fibonacci.js
Normal file
23
libs/utils/multithread/fibonacci.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { workerData, parentPort } from 'worker_threads'
|
||||||
|
|
||||||
|
let nums = [1, 1]
|
||||||
|
|
||||||
|
for (let i = 0; i < 42; 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("DONE!")
|
||||||
|
|
||||||
|
parentPort.postMessage(primes)
|
31
libs/utils/multithread/genericWorker.js
Normal file
31
libs/utils/multithread/genericWorker.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
import { workerData, parentPort } from 'worker_threads'
|
||||||
|
import initLog from '../log.js'
|
||||||
|
|
||||||
|
let script = ''
|
||||||
|
let myName = ''
|
||||||
|
|
||||||
|
let log = initLog(myName)
|
||||||
|
|
||||||
|
parentPort.on(
|
||||||
|
'message',
|
||||||
|
message => {
|
||||||
|
switch (message.type) {
|
||||||
|
case 'setName':
|
||||||
|
myName = message.name
|
||||||
|
log = initLog(myName)
|
||||||
|
log(`Changed name to '${myName}'.`)
|
||||||
|
break
|
||||||
|
|
||||||
|
case 'setScript':
|
||||||
|
script = message.script
|
||||||
|
log = initLog(myName)
|
||||||
|
log(`Changed name to '${myName}'.`)
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
|
@ -22,6 +22,6 @@
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mongoose": "^6.0.7",
|
"mongoose": "^6.0.7",
|
||||||
"sqlite": "^4.0.23",
|
"sqlite": "^4.0.23",
|
||||||
"sqlite3": "^5.0.2"
|
"sqlite3": "^4.2.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
tests/multihread.test.js
Normal file
3
tests/multihread.test.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { initWorkers, spread } from '../libs/utils/multithread.js'
|
||||||
|
|
||||||
|
initWorkers(8)
|
Loading…
Reference in New Issue
Block a user