I am struggling with node.js 'worker_threads'.
So what I wanted to do is to pass several instances of my custom class into the worker thread. Instances are assigned to map by some unique serial number. So basically, I have got a Map of type - <string, MyUniqueClassInstance>.
My worker implementation looks like this:
Class method running a worker service:
public static runService = (workerData:any) => {
return new Promise((resolve, reject) => {
const route = path.join(__dirname, '/worker.js');
const worker = new Worker(route, { workerData });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code:number) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
})
})
}
worker itself:
const { workerData, parentPort } = require('worker_threads')
const instance = new Counter();
const {items, myMap} = workerData;
const pResponseList:Promise<any>[] = [];
items.map((item: Item) => {
pResponseList.push(
instance.count(item, myMap.get(item._id)!)
);
});
Promise.all(pResponseList).then(res => parentPort.postMessage(res));
And whenever, inside 'count' method I try to run a method from item instance it throws an error
myMapEntry.myCustomInstanceMethod is not a function
I tried to console.log() content of my instance just before passing it to .count() method and everything is correctly settled.
Same pattern runs flawlessly outside of worker instance.
Could anyone help me to find out what exactly could potentially be wrong inside this code?
You can't pass functions (e.g. instances of classes won't work, at least their methods won’t) - you can only pass serializable data.
https://nodejs.org/api/worker_threads.html#worker_threads_worker_workerdata
An arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor.
The data is cloned as if using postMessage(), according to the HTML structured clone algorithm.
Then let's go to HTML structured clone algorithm
Things that don't work with structured clone Function objects cannot be duplicated by the structured clone algorithm; attempting to throws a DATA_CLONE_ERR exception.
Could you perhaps reconstruct the instance of the class using serialized data, call your methods, and then return the serialized data back to the parent thread?