The worker thread module has already become a stable module on Node.js version 12. This module enables us to run multiple Node.js processes in parallel using thread. In the past, we couldn't do this easily. We probably ended up utilizing cluster or spawning child process. The difference in utilizing thread is that we have shareable resources (memory). The main and its child threads can communicate and pass the operation results directly with a concept of message passing.
Child thread is usually used for distributing computation load in an application. The main thread may migrate a certain process to a child thread which is run a computational-expansive and asynchronous process. For instance, the following code shows how we can create a worker thread for a file reading process then send the result to the main thread.
// module.js
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
if (isMainThread) {
// if it is accessed as main thread, return a function that will create a child thread for file reading
module.exports = function readFileAsync(file) {
return new Promise((resolve, reject) => {
// load current file inside a worker instance
const worker = new Worker(__filename, {
// pass the file name to the worker
workerData: file
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
};
} else {
// if it is accessed by a worker
const fs = require('fs');
const path = require('path');
const file = workerData;
const content = fs.readFileSync(path.join(__dirname, '../files', file));
// content is transfered to parent thread as array of bytes
parentPort.postMessage(content);
}
As we have already created a module for defining the main function. Now, we can create a program that will utilize that function to read a file.
//app.js
const readFileAsync = require('./module');
readFileAsync('file1.txt')
.then(result => {
console.log('File content is:', Buffer.from(result).toString());
})
.catch(reason => {
console.log('File reading failed:', reason);
});
Comments
Post a Comment