I crushed onto a wall pretty violently. The thing is that I am calling a function in the main thread with the form:
const {StaticPool} = require("node-worker-threads-pool");
const friendsTableWorkers = (result) => {
const pool = new StaticPool({
size: 8,
task: "./worker.js",
workerData: result
})
const nums = [23, 25]
let buffer = []
const size = Int32Array.BYTES_PER_ELEMENT*nums.length
const sharedBuffer = new SharedArrayBuffer(size)
const sharedArray = new Int32Array(sharedBuffer)
nums.forEach((num, index) => {
Atomics.store(sharedArray, index, num);
})
pool.exec(sharedArray).then(res => {
buffer.push( res )
}).finally(()=>{
console.log( buffer )
})
return buffer
}
which, in turn, use a fixed pool of workers of the form:
const { parentPort, workerData } = require("worker_threads")
const _ = require('lodash')
const { idToEnsembl } = require('./supportFunctionsBackend.js')
function friendSeeker(n) {
console.log(n)
let res = workerData.map(x => x._doc)
return {
ensemblGeneId: idToEnsembl(n),
nodeidentifier: n,
geneSetFriends: _.countBy(res, (o) => {
return (o.nodes.includes(n))
}).true}
}
parentPort.on("message", (param) => {
param.forEach( friend => {
if (typeof friend !== "number") {
throw new Error("param must be a number.");
}
const result = friendSeeker(friend);
parentPort.postMessage(result);
})
})
But, when I run it, I get the following:
23
[
{
ensemblGeneId: 'ENSG00000000003',
nodeidentifier: 23,
geneSetFriends: 2249
}
]
25
When the real result should be:
[
{
ensemblGeneId: 'ENSG00000000003',
nodeidentifier: 23,
geneSetFriends: 2249
},
{
ensemblGeneId: 'ENSG00000000005',
nodeidentifier: 25,
geneSetFriends: 321
}
]
or
[
{
ensemblGeneId: 'ENSG00000000005',
nodeidentifier: 25,
geneSetFriends: 321
},
{
ensemblGeneId: 'ENSG00000000003',
nodeidentifier: 23,
geneSetFriends: 2249
}
]
Which means that just one worker is pushing his result into the buffer array. My question is, then, how can I make all the workers push their result into the buffer array? The order is not important.
Thank you very much in advance